Re: While loop and commands that read from std input



Am 14.09.2011 07:49, schrieb mb:
On Sep 14, 1:08 am, Barry Margolin<bar...@xxxxxxxxxxxx> wrote:
In article
<a6492475-30db-45a9-bec4-bb3361fde...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,



mb<mbsa...@xxxxxxxxx> wrote:
Hi,

I have been searching unsuccessfully for a deeper explanation on the
interaction between a 'while' loop and commands that read from std
input, such as ssh, ffmpeg, mplayer, ... , and how they consume all
the arguments of the loop and the usage of '/dev/null' to prevent such
unwanted behaviour, as in:

while read song
do
ffmpeg -i "$song" -ab 128kb low_"$song".mp3</dev/null
done< list

My current understanding is that there is some sort of design
difference between such commands and others that don't read std input.
Is my understanding correct or there is more to it ?

Thanks.

Commands inherit their file descriptors from the shell process that
invokes them. So in this case, the shell first redirects its own
standard input to the file. If you didn't have</dev/null ffmpeg would
inherit that standard input redirection, so when it wants to read
something it will read from the stream connected to the file. Anything
it reads will no longer be available to the shell.

--
Barry Margolin, bar...@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Thanks to both of you for your comments.
Maybe I wasn't too clear in my question ... what I wanted was which
are these commands that read from std input, for example ls, cat,
file, echo, ....How can I tell which one reads stdin and which does
not, beforehand ? Is there a man/info page of these or other commands
that tells you that ? To me doing:

Yes, there are man/info pages for the standard commands. (And hopefully
you find documentation for the non-standard ones.) "Beforehand" you can
never know since it depends on the actual program whether it reads from
stdin, from files, or both, and in the latter case with what precedence
they've implemented the read calls in the respective program. (What you
do know is how the shell passes its descriptors to the programs; which
has already been answered upthread.)

Janis


while read song
do
ffmpeg "$song"
done< list

and

while read File
do
cat $File
done< file_list

seem the same construct, but they behave differently, the first
silently fails (without /dev/null) and the second does not. That's my
confusion in grasping the essence of this behaviour.

.



Relevant Pages

  • Re: While loop and commands that read from std input
    ... the arguments of the loop and the usage of '/dev/null' to prevent such ... difference between such commands and others that don't read std input. ... beyond the normal, expected kind of redirection). ...
    (comp.unix.shell)
  • While loop and commands that read from std input
    ... the arguments of the loop and the usage of '/dev/null' to prevent such ... difference between such commands and others that don't read std input. ... Is my understanding correct or there is more to it? ...
    (comp.unix.shell)
  • Re: While loop and commands that read from std input
    ... the arguments of the loop and the usage of '/dev/null' to prevent such ... difference between such commands and others that don't read std input. ... inherit that standard input redirection, so when it wants to read ...
    (comp.unix.shell)
  • Re: While loop and commands that read from std input
    ... input, such as ssh, ffmpeg, mplayer, ... ... the arguments of the loop and the usage of '/dev/null' to prevent such ... difference between such commands and others that don't read std input. ...
    (comp.unix.shell)
  • [kde] Re: Dolphin service menu help: preserve file timestamp
    ... the effect is to loop thru the ... counts, so where awk can be used to avoid strings of several commands, ... first learned practical shell scripting by studying initscripts. ... before that, bemoaning the newfangled boot commands, as admins no longer ...
    (KDE)