LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script error (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-error-789971/)

_Linux_Learner 02-18-2010 12:21 PM

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.

pixellany 02-18-2010 12:38 PM

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


_Linux_Learner 02-18-2010 01:31 PM

hi pixellany

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

Thanks in advance

pixellany 02-18-2010 01:33 PM

Quote:

Originally Posted by _Linux_Learner (Post 3868424)
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.

pixellany 02-18-2010 01:38 PM

formatting examples:

Code:

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

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


catkin 02-19-2010 01:20 AM

It might also help if you explained what the symptoms of "it does not work" are.

_Linux_Learner 02-19-2010 03:22 AM

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

pixellany 02-19-2010 12:12 PM

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.

fusion1275 02-19-2010 12:31 PM

find . -ls

_Linux_Learner 02-20-2010 01:14 PM

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

colucix 02-20-2010 04:25 PM

Quote:

Originally Posted by _Linux_Learner (Post 3869072)
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 (Post 3869072)
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. ;)

_Linux_Learner 02-21-2010 12:21 PM

Solved
 
Hi colucix

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

regards
_Linux_Learner ;)


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