我收到命令流作为输入:
command1
command2
command3
由此,我想创建一个包含以下内容的文件output.txt
command1 output1
command2 output2
command3 output3
output_i
是command_i
的输出(每个命令返回一个整数)。我可以依次使用parallel
和paste
来做到这一点,但我想知道是否有办法在单个bash调用中获取output.txt
。
EDIT
使用parallel
,我就是这样做的:
cat commands.txt | parallel -k > outputs_only.txt
paste commands.txt outputs_only.txt > outputs.txt
答案 0 :(得分:2)
只是bash
中的循环,包含命令的文件的输入重定向
#!/bin/bash
while read -r line; do
echo "$line" "$(eval "$line")"
done < commands.txt > output.txt
或单行
while read -r line; do echo "$line" "$(eval "$line")"; done < commands.txt > output.txt
如果您想要从stdin
而不是从文件中读取,只需管道流到循环,
< command-producing-stream > | while read -r line; do echo "$line" "$(eval "$line")"; done > output.txt