Re: for loop (?)

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 01/30/04


Date: Fri, 30 Jan 2004 12:08:46 +0100

2004-01-30, 03:03(+00), Icarus Sparry:
[...]
> I am not aware of any shell which
> can handle NUL characters correctly in its input.

zsh does.

> For the particular
> characters you list ('\', '#', newline) you need to take special action to
> pass these characters to the script in the first place.

That's the same for every program written in any language.

[...]
> find $( ls | grep -v SOLARIS8 ) -type f -print | xargs awk '...'
>
> to process the source tree.
> It is silly to have to put in a sed script to transform any filenames with
> an '=' sign in them.

You wouldn't have the problem with
find $(ls ./* | grep -v...)

Note the zsh way:

zargs ./^*SOLARIS8*/**/*(.) -- awk '...'

(you need "setopt extendedglob", and "autoload -U zargs" but
this is generally already done when you use the shell
interactively).

[...]
> I have less problems with the '-v' approach, but this is not portable.
> In particular /usr/bin/awk on SOLARIS 8 does not support it. Yes it does have
> nawk, which does support it, but this is a different command to type.

You can also arrange to have /usr/xpg4/bin first in your PATH
(that should even be the default in SUS conformance mode).
Most Unices claim POSIX conformance now, so you may be able to
rely on the fact that utilities are conformant and be reliably
portable.

[...]
> For now I will continue to look at the problem, and see if it is worth the
> extra effort to make the script robust against weird inputs. My feeling
> is that for the vaste majority of the programs posed in comp.unix.shell
> the extra effort is not worthwhile

Yes, but it's good to point out the limitations and problems for
those of the readers for whom it might "be worthwhile".

> and for those where it is worthwhile
> then one should either use perl (python, ruby etc) or write a
> C program.

Yes, thank you to say so.

> Certainly for the trivial 'seq' I think my approach is correct. The program
> will not do anything sensible if given an arguement like "fred", so why
> should I care if it will do something sensible with an arguement like "'#'"?

Yes, the full equivalent of "seq" would have been longer
(arguments parsing, checking, steps...). Note that you didn't
need for "sh" (except you may have had trouble to decide for a
portable shebang line for awk).

[...]
> PRINTER=P*
> awk -v printer=$PRINTER '$0 ~ printer'
>
> you will get out a lot more than you might expect.
[...]

That would have been much more fun with

PRINTER=B*
awk -v printer=$PRINTER '$0 ~ printer'

if there had be a file named "B" and a file named
'BEGIN { system("rm -rf ~") }' in the current directory.

Except for zsh, you need to quote every shell variable.

But we mostly agree, except that I consider that when giving a
solution read by many people that may be reused in different
contexts, it's important to point out the limitations (at least
so that people realise it's generally a bad idea to use a shell
to perform this or that kind of task).

-- 
Stéphane                      ["Stephane.Chazelas" at "free.fr"]


Relevant Pages