Differences between revisions 2 and 3
Revision 2 as of 2008-04-09 00:47:52
Size: 1200
Editor: GreyCat
Comment: split the first two examples
Revision 3 as of 2008-05-22 23:33:39
Size: 1389
Editor: GreyCat
Comment: expand, adjust link(s)
Deletions are marked like this. Additions are marked like this.
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 16: Line 17:
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 22:
    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 27:
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 29:
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]).

Anchor(faq89)

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 [:BashGuide/TheBasics/InputAndOutput#Redirection: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 [http://www.openssh.org/ OpenSSH]).

BashFAQ/089 (last edited 2022-10-30 09:06:29 by emanuele6)