LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-04-2004, 07:24 AM   #1
ray5_83
LQ Newbie
 
Registered: Jul 2004
Posts: 13

Rep: Reputation: 0
Question 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
 
Old 08-04-2004, 07:33 AM   #2
rnicolson
Member
 
Registered: Jul 2004
Location: Winnipeg
Distribution: Slackware 11
Posts: 74

Rep: Reputation: 15
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.
 
Old 08-04-2004, 07:50 AM   #3
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
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.
 
Old 08-04-2004, 08:29 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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.
 
Old 08-04-2004, 05:44 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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".

Last edited by Hko; 08-04-2004 at 05:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing a bash script poiuytrewq Linux - Newbie 2 10-07-2004 10:26 PM
Help writing a pinging bash script dehuszar Linux - Software 3 06-18-2004 02:03 PM
Writing to a file - Bash script Skute Programming 2 03-15-2004 04:41 AM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 01:38 PM
writing bash script ankitgdit Programming 4 08-19-2003 06:47 AM


All times are GMT -5. The time now is 03:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration