LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Writing bash script with recursion.. (https://www.linuxquestions.org/questions/programming-9/writing-bash-script-with-recursion-213252/)

ray5_83 08-04-2004 07:24 AM

Writing bash script with recursion..
 
Can someone show me how to use recursion in bash script?
For example, i want to to change the file permission to executable for all the files inside the directory including files in the subdirectory?

Please give some helpful links if there is any..

BTW, in a bask script, how can we check if the user running this script is super user(root), since only super user can running this script to use the function?


cheers,
ray

rnicolson 08-04-2004 07:33 AM

First if you run chmod -R on a folder it will recursively go through all the directories and change the permissions. That should answer the first question.

Here are a few pages with bash tutorials/refrence materials
http://www.freeos.com/guides/lsst/
http://db.ilug-bom.org.in/Documentation/abs-guide/
and I am sure there are many more. Just do a google search for bash scripting and it comes up with a lot more.

With regards to checking withing the script if the user is root. I am not sure how to do that however the script will not perform the commands if the user is not root, or you could also just ch-mod the script so only root can run it.

ranger_nemo 08-04-2004 07:50 AM

If you really want to do it in Python, here's a bit of a start...
Code:

#!/usr/bin/python

import os

for fname in os.listdir(os.curdir):    # This will give you the ls of the current dir
  os.system("chmod a+x fname")          # This will set permission

You would have to create a function out of this. Have the program check if fname is a dir... If it is, call the function inside the new dir.

theYinYeti 08-04-2004 08:29 AM

Quote:

Can someone show me how to use recursion in bash script?
As in any language... Here's a (not so good) example:
Code:

# find files in directory $1 that are new compared to directory $2
function findNew() {
        # $3 is a private parameter for recursion (initial value: .)
        local R=${3:-.}

        if [ ! -d "${1}/$R" ]; then
                return
        elif [ ! -e "${2}/$R" ]; then
                cd "$1" && find "$R" -type f -depth -print
                return
        elif [ ! -d "${2}/$R" ]; then
                return
        fi

        for rep in $(cd "${1}/$R" && find . -maxdepth 1 -type d -mindepth 1 -exec basename "{}" \;); do
                findNew "$1" "$2" "${R}/$rep"
        done
        if [ "${1}/$R" -nt "${2}/$R" ]; then
                cd "$1" && find "$R" -maxdepth 1 -type f -newer "${2}/$R" -print
        fi
}

Quote:

For example, i want to to change the file permission to executable for all the files inside the directory including files in the subdirectory?
No need for recursion in this case. Simply do that:
Code:

find "/place/to/work/on" ! -type d -print | xargs chmod a+x
A simple "chmod -R" as suggested is dangerous because in some cases, that is not what you want. In this case it would have worked, but consider the following: you got plenty of mp3 from a CDROM, and all files have the execute bit set; for making those files non-executable, you may try: chmod -R a-x /the/directory; but then even the directories would loose the x bit, and then you could not go inside anymore!
Quote:

BTW, in a bask script, how can we check if the user running this script is super user(root), since only super user can running this script to use the function?
The best way to ensure that only root can execute your script is ("#" is root's shell prompt):
Code:

# chown root.root yourscript.sh
# chmod 700 yourscript.sh

("700" can be replaced by: "u=rwx,go=")

Yves.

Hko 08-04-2004 05:44 PM

As for the first question, you don;t need recursion to do that. Like rnicolson already said, you can use the -R to chmod to have it process the entire directory tree. If you want eXecutable only for directories, you can use u+X (note: uppercase) instead of u+x. E.g. "chmod -R u+rwX".

For the second question, how to check if a script is run by root:
Code:

#!/bin/bash

echo "Checking if you are root..."
if [ "$UID" == "0" ] ; then
        echo "OK, you are root!"
else
        echo "You're not root enough. Go away!"
fi

The user ID of root (and only root) is always 0. And bash makes the user ID available by the (readonly) var "$UID".


All times are GMT -5. The time now is 02:31 PM.