Read file as input for a command skipping lines

旧街凉风 提交于 2021-02-05 09:36:55

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36771443/read-file-as-input-for-a-command-skipping-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!