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

Re: Lonely spacecowboy

On Tue, Mar 27, 2007 at 03:01:42PM -0400, Brian K. White wrote:
> You did not say in what way it fails, nor what <cmd> might be.
> But I can say 2 things:
> 1) pipeing into read only "works" in ksh, the real korn ksh not pdksh or 
> any of the others like bash and zsh that have a "ksh compatibility" mode.
> But you can still use a similar construct with file redirection that will 
> work.
> The difference is "|read" creates a child process that read runs in, and 
> any env variables set in that child process are not visible from the 
> parent, and the while-do-done command (and all commands in that loop) ar 
> running in the parent.

While this is true for bash and pdksh it's not true for zsh:

~$ echo foo | read
~$ echo $REPLY

> You might be able to redirect stdin just for the cmd but I never tried that 
> in a loop like this so I don't know if it actually works.

Generally done as:

while IFS= read -r <&3; do
  cmd ... "$REPLY"
done 3< file.txt

(read without -r or with the default value of IFS is very
special, you generally don't want that in scripts).

> # test for no tty to allow for cron jobs, cgi script, print filter, etc...
> tty -s && MYTTY=`tty` || MYTTY=/dev/null
> while read
> do
>      vi "$REPLY" <$MYTTY
> done < files.txt

Even if there's a tty, there's no guarantee that stdin is
guaranteed. There's no need for that trick except if you're
using csh type shells that hardly have any fd handling

> You could also just use awk and not worry about stdin and tty and read 
> etc...
> no redirection or piping, no read command, no tty issues, etc...
> awk '{system("somecommand \""$0"\"")}' files.txt

Then you'd run into more problems (possibly serious) if the
lines in files.txt contain any ", \, $, `...

> It's a little less efficient because system() spawns a new sh process to 
> run command in, where the while-read loop will run command right in the 
> same top level shell process where the while loop itself is running.

If "<cmd>" is not builtin, there's be as many processes (unless
is not optimised to exec the last command is run). The
difference is on the loading and initialising of sh.


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