Reputation: 3842
I have the below code which has to do a recursive listing of all the files and directories. I am not sure if it is working fine.
#!/usr/bin/sh
recur_fun()
{
for i in `ls -ltr | awk '{print $9}'`
do
echo $i;
cd $i;
ls
pwd
recur_fun
cd ..
pwd
done
}
I need to copy the name of the file and then use it in clearcase.
Upvotes: 2
Views: 1408
Reputation: 1324208
Note: I would rather recommend going with cleartool command than OS-specific shell commands.
For instance:
cleartool ls -r -nxn
would list all files recursively, private or not.
See Command line to delete all ClearCase view-private files for more.
Upvotes: 2
Reputation: 753695
If the purpose is to get the job done, then use pre-built tools such as 'find
' or 'ls -R
' to do the job reliably.
Assuming that the purpose is to learn about recursion and shell scripting, rather than to get the job done, then it is not working fine for a number of reasons:
cd
will at best produce an error message.cd
works, you're apt to see the same files listed over, and over, and over again (once per file in a given directory).Additionally, the two semi-colons are superfluous.
If you do need to use cd
in a script, it is usually a good idea to do that in sub-shell:
if [ -d $i ]
then ( cd $; pwd; recur_fun )
fi
When the sub-shell completes, you know the parent is still in exactly the same place it was in before - it doesn't depend on the vagaries of what the cd
command does across symlinks. Modern shells (meaning bash
) seem to think that you should normally want a 'logical cd
' operation, which bugs the hell out of me because I almost never do want that behaviour.
Upvotes: 1