[nzlug] bash help - for loop listing dirs

Liz liz at debian.co.nz
Thu Feb 22 08:03:37 NZDT 2007


If nobody else has responded later ill go more into this

for the fix for the problem with spaces try using a while loop

ie
ls -l | while read dir
do
cd "$dir"
done



Liz

On Thursday 22 February 2007 07:31, Howard wrote:
> Hi, I'm after some bash script help please...
>
> I have a for loop which I thought would need to be thus:
>
> for i in `ls -1`
> do
> cd $i
> blah...
>
> I need to recursively list dirs, then do some stuff.  Some dirs have
> spaces in their names, and these are listed as separate words in the
> loop... causing the dir names to be incorrect (breaks in for loop occur
> at spaces).
>
> I have briefly googled and had a suggestion to use IFS or maybe tr to
> changes the spaces to something else first... and not being a skilled
> basher can someone give me an example please of an 'accepted' way to do it?
>
> Thanks
> H
>
> _______________________________________________
> NZLUG mailing list NZLUG at linux.net.nz
> http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug




More information about the NZLUG mailing list