LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2014, 07:27 PM   #1
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Rep: Reputation: Disabled
Help with a recursive bash script


I am in the only Linux class offered at my school, and there is an assignment that is driving me nuts. I have to create a bash script that has a function that can be called recursively to scan the directory specified in the first argument, and list the any file with the setuid in ls -l format. If another directory is found the script should recall itself with the new path.

I thought that I had an idea of how to it using a for loop with if statements to test the directory for the stick bits and the sub-directories, but I cannot even get the function to list the file that I created with the sticky bit. I know this is way wrong, but this is how i started my script just to test for the sticky bit.

#!/bin/bash

setuid(){

for file in $1; do

if [ -u $file ]; then

ls -l $file
fi

done
}

setuid $1

Like I said, this is terrible and I have a feeling that I do not know what I'm actually doing. I am a super noob, and any help is appreciated.
 
Old 03-03-2014, 07:30 PM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,324
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
$ help while

And $1 may not be set.

if [ ! $1 ]; then
fi
 
Old 03-03-2014, 07:36 PM   #3
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the quick response. I didn't set $1 just in this example, but have tried the directory that I created the file in and I cannot get i to work. You think that I should use a while loop instead?
 
Old 03-03-2014, 09:29 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please tell us what is the value of $1?

Also, currently this has no sign of recursiveness ... is this coming after you solve this part?

---------- Post added 04-03-14 at 10:29 ----------

Please tell us what is the value of $1?

Also, currently this has no sign of recursiveness ... is this coming after you solve this part?
 
Old 03-03-2014, 09:43 PM   #5
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
The value of $1 will be a directory or "." if no argument is specified. You are correct the I haven't got ti the recursion yet. However, if you have any tips on making it recall itself when a directory is encountered and the new directory path included in the argument it would be greatly appreciated.
 
Old 03-03-2014, 09:49 PM   #6
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
I just realized that I had mixed a couple of things up. I don't need the sticky bit. just the setuid.
 
Old 03-03-2014, 09:56 PM   #7
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
I know it said not to do this in the rules, but maybe I am not explaining what I need to do very well. Here is the full problem.

Write out a bash script called 'b.sh' that has a function that can be called recursively to scan the directory specified by it's first argument ($1) for any file that is setuid and print out that file in ls -l format. For any directory it encounters the function should call itself with the new computed path. This function should be called initially with the first argument supplied to the script or "." if no argument is supplied to the script.
 
Old 03-03-2014, 10:37 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So let us step back a little for a sec. If you supply something like '.', your for loop will look like:
Code:
for file in .
do
   ...
done
So this will run the contents of the for loop for exactly 1 iteration using '.' as the only thing to be tested.

Now if we wanted to look at all the items inside the '.' directory, we would need something like:
Code:
for file in $1/*
do
   ...
done
Now the glob will expand to all items, files and directories, within that top directory.

As for the recursion, firstly you need to remember to test what type of item you are looking at, ie so that once a directory is found you can recall your function.
Another thing to remember, scope of variables will play a part (if you are not careful)
 
1 members found this post helpful.
Old 03-03-2014, 11:15 PM   #9
yimmy1890
LQ Newbie
 
Registered: Mar 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you, grail. That gets me started in the right direction. Its getting kind of late here, so I will have to continue with this in the morning. Any other tips you might think of that can help me along the way would be greatly appreciated. I will post back on here tomorrow if I am still stuck. Thanks!
 
Old 03-04-2014, 12:02 AM   #10
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,324
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
so....

if [ ! $1 ]; then
FIRST_PARM="."
else
FIRST_PARM="$1"
fi

for FILE in $FIRST_PARM .....etc.....

If you check $1 and it has not been passed, then your script will fail. Where $1 is the first parameter. And $0 is the name of the script. Also bear in mind that some distros like debian default to dash. Even if you set your shell to bash, when you launch a script with sh or ./ it will run under dash. Unless you run it "bash script.sh". It probably wont affect your low level thing, but some bash extras do not function under dash. Like ${VAR:1:1} to parse parts of strings.
 
Old 03-04-2014, 12:27 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
@Shadow_7 - The below is invalid information based on the interpreter already having been set inside the script:
Quote:
Even if you set your shell to bash, when you launch a script with sh or ./ it will run under dash. Unless you run it "bash script.sh".
By defining the interpreter at the start of the script, once executed it will use that interpreter (in this case the bash shell) to process the data inside the script.

Also, without a glob, your for loop will also not function as expected.
 
Old 03-04-2014, 08:49 AM   #12
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,324
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Quote:
Originally Posted by grail View Post
@Shadow_7 - The below is invalid information based on the interpreter already having been set inside the script:

By defining the interpreter at the start of the script, once executed it will use that interpreter (in this case the bash shell) to process the data inside the script.

Also, without a glob, your for loop will also not function as expected.
It depends on the distro and the day of the week I guess. For me:

$ export TEMP=ABCDEF; export TEMP2=${TEMP:2:2}; echo $TEMP "-" $TEMP2

would work on CLI, but not in a script. In debian sid if launched with sh or ./ it fails via Bad substitution (as it would in dash), even with the #! /bin/bash as the header. But if launched with bash it executes as expected. If done on the cli in bash it works. If done on the cli in dash (debians default) it fails. For all intents the interpreter line at the top of the script is ignored (in debian). In theory it shouldn't be, but in practice it appears to be.
 
Old 03-04-2014, 09:39 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well I will have to believe you as I am not familiar with Debian, although it does seem to fly in the face of any other distribution I have used and the whole point of defining an interpreter.

Out of curiosity, what if the interpreter is perl or python, does it ignore these too?

I am not sure why you use export in your example either, is there a reason you cannot simply assign the values?
I mean your example makes sense as it is on the cli, hence subject to whichever shell you are in.
 
Old 03-04-2014, 02:09 PM   #14
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,324
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
If I add the +x permission and use ./ it uses the interpreter line. Otherwise sh uses dash and bash uses bash, regardless of what the interpreter line says. And ./ wont run at all without the +x permission.

I can use just the var name in script, and I guess it works on the cli for the example without the export. But doesn't work if you're wanting something like LANG set and passed when you chroot using a one liner.
 
Old 03-04-2014, 03:43 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
When processing directories recursively, watch out for running out of parameters on large directories.

This is what the find command does quite well.

I realize this is a homework problem and the directory tree will almost certainly be limited in depth and width.
 
  


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
Bash or Perl Script - recursive search. vinnat Linux - Newbie 1 10-06-2011 09:51 AM
bash script to create folders including making recursive folders.... linux-bandit Linux - Software 8 11-28-2009 02:50 AM
Problem with recursive bash script me4linux Programming 6 03-20-2007 06:46 AM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 08:03 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 02:52 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration