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

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 20 Sep 2012 11:16:40 +0200, Jan Henrik Sylvester wrote:
> 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.

You are fully correct: In order to create an iterator that
provides "valid" directory names, even for the cases where
"unusual" characters (which are _valid_ characters for file
names and directory names) are included, is not trivial.

Allow me to point to those two articles which mention different
approaches and show why they are wrong. :-)

David A. Wheeler:
Filenames and Pathnames in Shell: How to do it correctly
http://www.dwheeler.com/essays/filenames-in-shell.html

David A. Wheeler:
Fixing Unix/Linux/POSIX Filenames:
Control Characters (such as Newline), Leading Dashes, and Other Problems
http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html



> 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.

It will work - it delegates resolving * to the shell instead
of having a different program (ls | grep, find) doing that.





-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120920172618.607c8ed3.freebsd>