From: Thomas 'PointedEars' Lahn on
Kenny McCormack wrote:

> In this group, it is an FGA that one should so:
>
> commandThatGeneratesTheFilenames | while read x ...
>
> instead of the more common:
>
> for i in ...
>
> because of the problems of filenames that spaces and other weird
> characters.
>
> However, one downside to the "while" method is that inside the loop,
> stdin is now coming from the pipe, and commands that we execute inside
> the loop, that expect a normal stdin, will misbehave.

Name one, and I am pretty sure that I can tell you the switch that will
prevent it from considering stdin. For example, ssh(1) has `-n'.

--
PointedEars
From: Thomas 'PointedEars' Lahn on
Kenny McCormack wrote:

> In this group, it is an FGA that one should so:
>
> commandThatGeneratesTheFilenames | while read x ...
>
> instead of the more common:
>
> for i in ...
>
> because of the problems of filenames that spaces and other weird
> characters.

No, the foregone assumption (FGA is not a common Usenet acronym -- shouldn't
that be "foregone conclusion" anyway?) is that find(1) with `-exec', or
`find' with xargs(1), are safer than both approaches.

Usenet rule of thumb: Never ask what is better; bottom line is that better
is what works for you. Ask instead what is more efficient aso. given a
specific context.

--
PointedEars
From: John DuBois on
In article <i243tu$l8g$1(a)news.xmission.com>,
Kenny McCormack <gazelle(a)shell.xmission.com> wrote:
>In this group, it is an FGA that one should so:
>
> commandThatGeneratesTheFilenames | while read x ...
>
>instead of the more common:
>
> for i in ...
>
>because of the problems of filenames that spaces and other weird characters.
>
>However, one downside to the "while" method is that inside the loop,
>stdin is now coming from the pipe, and commands that we execute inside
>the loop, that expect a normal stdin, will misbehave.
>
>Workarounds:
> 1) append "< /dev/tty" to each command
> 2) enclose the commands in parens and redirect that. I.e.:
>
> commandThatGeneratesTheFilenames | while read x;do
> (cmd1;cmd2;cmd3;...) < /dev/tty
> done
>
>Method 2 is nice and seems to work fine, but I am wondering if there is
>any hidden cost to it and if there is any more elegant/effcient way to
>do this.

Unless you specifically want to read from the controlling tty rather than
the standard input, it's always better to save the standard input to
another fd and then read from it.

{
commandThatGeneratesTheFilenames |
while read x <&4; do
cmd1
cmd2
cmd3
done 4<&0 0<&3
} 3<&0

In modern shells (ksh93/bash/zsh) you can do:

while read -u3 x; do
cmd1
cmd2
cmd3
done 3< <(commandThatGeneratesTheFilenames)

John
--
John DuBois spcecdt(a)armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
From: Kenny McCormack on
In article <B_qdnbdAifw8u9rRnZ2dnUVZ_s6dnZ2d(a)speakeasy.net>,
John DuBois <spcecdt(a)armory.com> wrote:
....
> Unless you specifically want to read from the controlling tty rather than
> the standard input, it's always better to save the standard input to
> another fd and then read from it.
>
> {
> commandThatGeneratesTheFilenames |
> while read x <&4; do
> cmd1
> cmd2
> cmd3
> done 4<&0 0<&3
> } 3<&0

What's the difference between enclosing the cmds in {} vs. ()?

--
Faced with the choice between changing one's mind and proving that there is
no need to do so, almost everyone gets busy on the proof.

- John Kenneth Galbraith -

From: Chris F.A. Johnson on
On 2010-07-21, Kenny McCormack wrote:
> In article <B_qdnbdAifw8u9rRnZ2dnUVZ_s6dnZ2d(a)speakeasy.net>,
> John DuBois <spcecdt(a)armory.com> wrote:
> ...
>> Unless you specifically want to read from the controlling tty rather than
>> the standard input, it's always better to save the standard input to
>> another fd and then read from it.
>>
>> {
>> commandThatGeneratesTheFilenames |
>> while read x <&4; do
>> cmd1
>> cmd2
>> cmd3
>> done 4<&0 0<&3
>> } 3<&0
>
> What's the difference between enclosing the cmds in {} vs. ()?

Commands in ( ) are executed in a subshell.


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)