Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for i in `ls` 1

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
0
0
US
Good Day,

Our script looks like:

cd dirX;
for i in `ls`
do
cd $i
done;

For some reason it doesn't reach all the directories under dirX.

Any ideas?

Thanks,
Dan
 
It seems that the magic number is 30. Meaning, it loops only through 30 directories.
 
There could be a limit on the length on the string that can be returned by a backquote commande or a limit on the length of a command (here the [tt]for i in[/tt] command).

There could be non directory entry in dirX (simple files).

Also, there could be a problem if any subdirectory has 'special' characters embedded. Special characters can be '*', ';', ' '(space) or even '\n'(newline) ... Quote every usage of your variable.

Try :
Code:
ls | while read dir; do
    if [ -d "$dir" ]; then
        cd "$dir"
    fi
done

Or:
Code:
ls -d */. | while read dir; do
    cd "$dir"
done


P.S.: I do not understand how your script (and my examples too) could work even for the second directory: once you 'cd' to the first subdirectory you are no more in dirX so you could not 'cd' into the second one. I just hope you give us a small subset of your script and that in the full script you go back to dirX at the end of the loop.

--------------------

Denis
 
Denis,

You are absolutely right! We had some non directory entries in dirX which we fixed using via:

for i in `ls -F | egrep "/$"`

Thanks,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top