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 |
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. |
If you really want to do it in Python, here's a bit of a start...
Code:
#!/usr/bin/python |
Quote:
Code:
# find files in directory $1 that are new compared to directory $2 Quote:
Code:
find "/place/to/work/on" ! -type d -print | xargs chmod a+x Quote:
Code:
# chown root.root yourscript.sh Yves. |
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 |
All times are GMT -5. The time now is 02:31 PM. |