Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.