当然可以。假设这是input.txt
:
source1.txt dest1.txt
source2.txt dest2.txt
...
而要做到这一点:
command source1.txt dest1.txt
command source2.txt dest2.txt
...
这里有一个办法:
while read i o; do
command $i $o
done < input.txt
这假定命令command
已经构建读取从它的第一争论并写入第二个。如果command
改为打印到标准输出(即,到终端屏幕),则将command $i $o
替换为command $i > $o
。这也假定input.txt
中没有空格或有趣的字符。
如果您的input.txt
包含例如百万行或多个:
awk '{printf "command %s\n", $0}' input.txt | sh
或者,如果你必须使用command $i > $o
:
awk '{printf "command %s > %s\n", $1, $2}' input.txt | sh
这种方法从input.txt中,并打印command source1.txt dest1.txt
线的第一线,command source2.txt dest2.txt
第二,等...然后它“管”(|
)这些命令sh,执行它们。
对于错误command
处理,尝试:
while read i o; do
command $i $o || command2 $i $o >> command2.log
done < input.txt 2> error.log
或者:
done <input.txt> error.log 2>&1
(其中之一将更好地工作,这取决于是否command
和command2
打印他们的错误到标准输出(1)或者stderr(2)。)
谢谢韦伯,我试过它有效。但是我在某些命令中失败了,是否有任何方法可以检查结果,如果失败了,我们应该在其中轻微修改来运行命令。每条命令执行完日志详细信息后都会加载到日志文件中。像下面一样读到i o;做 命令$ I $Ø \t如果结果(好) \t下一个迭代 \t其他 \t命令2 $ I $Ø 做< input.txt >输出log.txt的 – Hemanth
加入请求的错误处理 – webb