Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Sep 2012 11:16:40 +0200
From:      Jan Henrik Sylvester <me@janh.de>
To:        Polytropon <freebsd@edvax.de>,  Martin McCormick <martin@dc.cis.okstate.edu>
Cc:        questions-list freebsd <freebsd-questions@freebsd.org>
Subject:   Re: Re: bash Shell Scripting Question
Message-ID:  <505ADEF8.9080504@janh.de>
In-Reply-To: <20120920042945.ed2edd11.freebsd@edvax.de>
References:  <201209200203.q8K23Bv5034624@x.it.okstate.edu> <20120920042945.ed2edd11.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On 09/20/2012 04:29, Polytropon wrote:
> Correct. You could use different approaches which may or may
> not fail due to the directory names you will encounter (like
> directories with spaces or special characters).
>
> 	#!/bin/sh
> 	for DIR in `ls -LF | grep \/`; do
> 		cd ${DIR}
> 		# do stuff
> 	done
>
> Or you can use piping:
>
> 	#!/bin/sh
> 	ls -LF | grep \/ | while read DIR; do
> 		cd ${DIR}
> 		# do stuff
> 	done
>
> I'm quite confident there are even more elegant and fault-
> tolerant solutions. You would maybe have to tweak the ls
> command or play with IFS (space or newline).

Even if you start quoting "${DIR}", the first one will fail at least for 
names containing spaces, the second one at least for names starting with 
spaces. As you said, you would have to change IFS to maybe slash and 
newline, assuming that you do not have names containing newlines, in 
which case the approach cannot work.

I understand that you want all directories and links to directories not 
starting with a period. How about trying all files not starting with a 
period and skipping the non directories:

#!/bin/sh
for DIR in *
do
     cd "$DIR" >/dev/null 2>&1 || continue
     pwd
     cd - > /dev/null
done

This one works with names containing spaces or even newlines and does 
not even need to spawn external commands or subshells. It may have other 
caveats, though.

Cheers,
Jan Henrik



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?505ADEF8.9080504>