读取文件作为命令跳过行的输入 [英] Read file as input for a command skipping lines
问题描述
我正在尝试使用文本文件的内容作为命令的输入.我知道如何读取文件就好.但是,当我将读取行传递给要执行的命令时,脚本开始跳过其他每行.
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.
给出一个名为 queue
的纯文本文件:
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
输出:
one
two
three
four
但是,当我将 $ input
传递给命令时,每隔一行将被跳过:
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
我设法通过首先将整个文件读入数组然后在数组上进行迭代来使脚本正常工作,但是我仍然不明白为什么直接循环遍历文件不起作用.我以某种方式假定文件指针正在高级,但是我无法弄清楚原因. transcode-video
是红宝石.执行ruby程序时,我是否不知道幕后发生了什么?gem的作者提供了一个示例脚本,该脚本实际上是使用 sed
命令从文件中去除行的,并且效果很好.
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?
推荐答案
启动的应用程序尝试处理stdin,并读取一行.试试:
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.
这篇关于读取文件作为命令跳过行的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!