LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-18-2010, 12:21 PM   #1
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Rep: Reputation: 15
shell script error


Hi all

I am writing a shell script to traverse all directories present in current directory and list all the files in them.

I wrote the following script but it does not work...

traverse()
{
list=$1
path=$2

for i in $1
do

if [ -d $i ]
then
temp=$2
p=$temp"/"$i
l=`ls $p`
traverse "$l" "$p"
else
echo -e "\n\n ******************** $path ************************** \n"
echo -e "\n$i"
fi
done
return
}

curr_dir=`pwd`
list=`ls`
traverse "$list" "$curr_dir"

Please help...

Thanks in advance.
 
Old 02-18-2010, 12:38 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
to list all files and directories, starting with the current location:
Code:
ls -lR
starting with an arbitrary location:
Code:
ls -lR <pathname>
e.g.:
ls -lR /etc
 
Old 02-18-2010, 01:31 PM   #3
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Original Poster
Rep: Reputation: 15
hi pixellany

Thanks for your useful reply. But can you tell me the error in my script that I wrote...

Thanks in advance
 
Old 02-18-2010, 01:33 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by _Linux_Learner View Post
hi pixellany

Thanks for your useful reply. But can you tell me the error in my script that I wrote...

Thanks in advance
Maybe....If you will clean up the formatting and put it in code tags**, I'll take a look.

**advanced mode--select the text and click on the "#" in the toolbar.
 
Old 02-18-2010, 01:38 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
formatting examples:

Code:
for var in <expression>; do
    stuff
    more stuff
done

if [ <expression> ]; then
    stuff
    more stuff
else; then
    other stuff
fi

Last edited by pixellany; 02-18-2010 at 02:16 PM. Reason: boo-boo: elif instead of else
 
Old 02-19-2010, 01:20 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
It might also help if you explained what the symptoms of "it does not work" are.
 
Old 02-19-2010, 03:22 AM   #7
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Original Poster
Rep: Reputation: 15
revised

Sorry for the inconvenience caused to others.

Revised code......

Code:
traverse()
{
      list=$1
      path=$2

      for i in $1
      do

            if [ -d $i ]
            then
                  temp=$2
                  p=$temp"/"$i
                  l=`ls $p`
                  traverse "$l" "$p"

            else
                  echo -e "\n\n **** $path ****"
                  echo -e "\n$i"
            fi
      done
return
}

curr_dir=`pwd`
list=`ls`
traverse "$list" "$curr_dir"
This code is expected to traverse the current directory and print out all the files. If a directory is found it should enter inside that and do the same thing until all files are found.

For example consider the following directory structure:
desktop->linux->learner

This code if run in desktop folder will successfully enter and print all files in linux folder but will not enter into learner folder. This will simply print the name of learner folder which I don't want it to do.

regards
_Linux_Learner
 
Old 02-19-2010, 12:12 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I'm pretty sure that recursion is legal in a BASH function, but it gives me a headache trying to walk thru it......

One thing I spotted:
p=$temp"/"$i ##You might want to run some tests to verify that this does what you intend. It may be necessary to say: "p=${temp}...etc.

More generally, the way to trouble-shoot something like this is to insert some echo statements to make sure that variables are doing what you intended.
 
Old 02-19-2010, 12:31 PM   #9
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Mint 13
Posts: 265

Rep: Reputation: 35
find . -ls
 
Old 02-20-2010, 01:14 PM   #10
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Original Poster
Rep: Reputation: 15
p=$temp"/"$i

This p will actually hold the path of next directory to be searched if i is directory. I t shows the right value also. But I don't know why gives wrong answer.

regards and thanks
_Linux_Learner
 
Old 02-20-2010, 04:25 PM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by _Linux_Learner View Post
This code if run in desktop folder will successfully enter and print all files in linux folder but will not enter into learner folder.
This is because $i results in a name which is relative to the current top-level directory. In other words it descends into the directory tree, it finds "learner" and it checks if "learner" is a directory under "Desktop". This should solve the issue:
Code:
if [ -d $path/$i ]
Quote:
Originally Posted by _Linux_Learner View Post
This p will actually hold the path of next directory to be searched if i is directory. I t shows the right value also. But I don't know why gives wrong answer.
If I understand well, you mean that the displayed path is always the same. Take in mind that $path is continually updated every time it descends into a directory and the first time it's printed out by the echo statement, it retains the last assigned value (that is the path of the deepest level). If you put the variable in a local scope, you can avoid the problem:
Code:
local path=$2
Maybe the final result is not what you expect, but at least you can go one step further.
 
Old 02-21-2010, 12:21 PM   #12
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Original Poster
Rep: Reputation: 15
Wink Solved

Hi colucix

Thanks for your valuable reply. It works now. Thanks to others also.

regards
_Linux_Learner
 
  


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
Shell Script that has an error to figure out deven1174 Programming 8 12-05-2009 03:25 PM
error in running the shell script bhandu Linux - Newbie 1 07-10-2007 02:16 AM
Help With A Shell Script Error PhantomFX Linux - Newbie 3 02-17-2005 05:42 AM
Shell Script Error Gerardoj Linux - General 2 12-03-2003 10:44 AM
Shell script error. chuck77 Linux - General 3 11-30-2001 12:30 AM


All times are GMT -5. The time now is 04:01 PM.

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