Differences between revisions 1 and 6 (spanning 5 versions)
Revision 1 as of 2008-04-08 23:25:13
Size: 1196
Editor: 82-71-12-170
Comment:
Revision 6 as of 2009-03-16 20:31:13
Size: 1737
Editor: localhost
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq89)]] <<Anchor(faq89)>>
Line 3: Line 3:
Typically you'll see this behaviour in situations like these:
Line 4: Line 5:
Typically you'll see this behaviour in situations like these:
Line 7: Line 7:
    ffmpeg -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 "${file%.avi}".mkv     ffmpeg -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 \
      
"${file%.avi}".mkv
Line 9: Line 10:
}}}
Line 10: Line 12:
  while read host
 
do
{{{
while read host; do
Line 16: Line 18:
What's happening here? Let's take the first example. `read` reads a line from standard input (FD 0), puts it in the file parameter, and then ffmpeg is executed. Like any program you execute from BASH, ffmpeg inherits standard input, which for some reason it reads. I don't know why. Here's how you make it work: What's happening here? Let's take the first example. `read` reads a line from standard input (FD 0), puts it in the file parameter, and then `ffmpeg` is executed. Like any program you execute from BASH, `ffmpeg` inherits standard input, which for some reason it reads. I don't know why. But in any case, when `ffmpeg` reads stdin, it sucks up all the input from the `find` command, starving the loop.

Here's how you make it work:
Line 19: Line 23:
    ffmpeg </dev/null -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 "${file%.avi}".mkv     ffmpeg </dev/null -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 \
      
"${file%.avi}".mkv
Line 23: Line 28:
Notice the redirection on the ffmpeg line: `</dev/null`. See the [:BashGuide#Redirection:redirection section] of the BASH Guide for more information on this. Notice the redirection on the ffmpeg line: `</dev/null`. See the [[BashGuide/TheBasics/InputAndOutput#Redirection|redirection section]] of the BashGuide for more information on this.
Line 25: Line 30:
The ssh example can be fixed the same way, or with the `-n` switch (at least with `openssh`). The ssh example can be fixed the same way, or with the `-n` switch (at least with [[http://www.openssh.org/|OpenSSH]]).

Sometimes with large loops it might be difficult to work out what's reading from stdin; or a program might change its behaviour when you add `</dev/null` to it. In this case you can make read use a different file descriptor that a random program is less likely to read from:
{{{
  while read <&3 line; do
    ......
  done 3<file
}}}

I'm using a loop which runs once per line of input but it only seems to run once; everything after the first line is ignored?

Typically you'll see this behaviour in situations like these:

  while IFS= read -r file; do
    ffmpeg -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 \
      "${file%.avi}".mkv
  done < <(find . -name '*.avi')

  while read host; do
    ssh "$host" command
  done <hostslist

What's happening here? Let's take the first example. read reads a line from standard input (FD 0), puts it in the file parameter, and then ffmpeg is executed. Like any program you execute from BASH, ffmpeg inherits standard input, which for some reason it reads. I don't know why. But in any case, when ffmpeg reads stdin, it sucks up all the input from the find command, starving the loop.

Here's how you make it work:

  while IFS= read -r file; do
    ffmpeg </dev/null -i "$file" -vcodec libxvid -acodec libfaac -ar 32000 \
      "${file%.avi}".mkv
  done < <(find . -name '*.avi')

Notice the redirection on the ffmpeg line: </dev/null. See the redirection section of the BashGuide for more information on this.

The ssh example can be fixed the same way, or with the -n switch (at least with OpenSSH).

Sometimes with large loops it might be difficult to work out what's reading from stdin; or a program might change its behaviour when you add </dev/null to it. In this case you can make read use a different file descriptor that a random program is less likely to read from:

  while read <&3 line; do
    ......
  done 3<file

BashFAQ/089 (last edited 2024-04-13 21:56:35 by Reg)