Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: somehow offtopic: looping filenames



Hi,

> There is nothing about this, that would improve the behaviour of a
> simple for loop like
> 
>   for i in *; flac -- $i
> 
> (That's `short_loops' syntax, which is on by default in zsh. And it
> relies on `sh_word_split' being unset, which is also the default
> behaviour in zsh.)

I agree to this. Also it's nice to pass a variable without being
required to always specify it as ${foobar[@]}. Even better: to remind
myself of the fact that a for loop often deals with arrays I became
used to the following syntax:

for i (*) echo $i

and

for i ($foobar) { command1; command 2 || print "command 2 failed!" }

and

if [[ -n $foo ]] {
  bar
} else {
  baz
}

These are arbitrary examples of course; my point is that this syntax
makes scripts more readable to me than the agonising

if foo
then
  bar
else
  baz
fi

Especially with vim that highlights the opposite char ({) when you move
to the original char (}).

> Worse, it's *less* robust, because it's treating newline characters
> specially, although they are perfectly legal characters in a file name
> (albeit not very common ones). Also, if the user has an alias in
> place, like say,
> 
>   alias ls='ls -F'
> 
> ...that is not going to work either, because that will cause ls to
> append a / to directory names, a * to executable files and so on.

I remember trying to use aliases in a script. I don't think this works,
I changed them to functions at least. But yes I do agree on this, here
it even became a sport to minimise the use of external commands as much
as possible.

With zsh these tasks are often trivial. I suppose we all love examples
that show zsh's power and what I found great is that even sed and awk
can easily be replaced: 

cat $foobar | sed 's,Alice,Bob,g')  versus
${foobar/Alice/Bob}  (yes, bash can do this, too)

cat /proc/sys/kernel/version | awk '{print $2}'  versus
${$(</proc/sys/kernel/version)[(w)2]}.

> I know there are HOWTOs and tutorials online, that advocate constructs
> like that. And even worse ones, like "for i in `ls`; do...". But that
> doesn't make them good examples of shell programming.

Awfully written shell scripts have this much too often.

> I don't mean to offend with this reply. And I hope that my point is
> making sense to you. I've written about this in the past² (but it's in
> German) and so has the guy who maintains the FAQ for #bash (the IRC
> channel for GNU bash on freenode)³. Refer that that for more details
> on parsing the output from `ls'.

Additionally - it's a bit odd to prefer anything else above zsh's own
powerful globbing abilities. I have used 'ls -dl /proc/**/*keyword*(.)'
so often. I've often wondered if there is a Linux distribution using
zsh as its default shell for users and distribution specific stuff like
init scripts.

I found your email nice to read, Frank.

Ciao,
-Mark.



Messages sorted by: Reverse Date, Date, Thread, Author