0

I'm trying to use the contents of a text file as the input for a command. I know how to read a file just fine. However, when I pass the read line to the command I want to execute, the script starts skipping every other line.

Given a plain text file named queue:

one
two
three
four

This prints out each line as expected:

queue=`pwd`/queue
while read input; do
  echo $input
done < $queue

output:

one
two
three
four

However, when I pass $input off to the command, every other line is skipped:

queue=`pwd`/queue
while read input; do
  echo $input
  transcode-video --dry-run $input
done < $queue

output (transcode-video outputs a bunch of stuff, but I omitted that for brevity. I don't believe it is relevant):

one
three

I managed to get my script working by first reading the whole file into an array and then iterating over the array, but I still don't understand why directly looping over the file doesn't work. I'm assuming the file pointer is getting advanced somehow, but I cannot figure out why. transcode-video is a ruby gem. Is there something I'm not aware of going on behind the scenes when the ruby program is executed? The author of the gem provided a sample script that actually strips lines out of the file using a sed command, and that works fine.

Can someone explain what is going on here?

Jordan
  • 4,133
  • 1
  • 27
  • 43
  • 1
    This is effectively a duplicate of [this question](http://stackoverflow.com/q/9393038/258523) and its duplicates. – Etan Reisner Apr 21 '16 at 14:18

1 Answers1

3

The launched app tries to process stdin, and reads a line. Try:

transcode-video --dry-run $input </dev/null

Or check the manual for a command-line flag that does the job.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Holy crap. That's it! I assumed it would _use_ $input as the stdin input. Thanks! – Jordan Apr 21 '16 at 13:46
  • @JordanBondo Command line arguments are not standard input. And it is quite possibly using the `$input` argument *and* reading standard input. Many commands do that. – Etan Reisner Apr 21 '16 at 14:17
  • @EtanReisner Oh right, der. I knew that (if I actually _bothered_ to take 2 seconds to think about it) – Jordan Apr 21 '16 at 14:19