2017-04-25 72 views
1

我收到作为输入的命令流:执行命令和存储输出的列表到一个列表

command1 
command2 
command3 

由此,我想创建包含文件output.txt

command1 output1 
command2 output2 
command3 output3 

其中output_icommand_i的输出(每个命令返回一个整数)。我可以先后使用parallelpaste这样做,但我想知道是否有办法在单个bash调用中获得output.txt

编辑 与parallel,这是我要做的事:

cat commands.txt | parallel -k > outputs_only.txt 
paste commands.txt outputs_only.txt > outputs.txt 
+0

你能告诉你如何使用'parallel'解决这个问题? – choroba

+0

当然,我更新了。 – dhokas

+0

你有两个独立文件的命令和输出吗? – Rogus

回答

2

bash只是一个环路上包含命令的文件输入重定向,

#!/bin/bash 

while read -r line; do 
    echo "$line" "$(eval "$line")" 
done <commands.txt> output.txt 

或者在单行 as

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 
+0

谢谢。是否有一个等价物可以让我在不创建'commands.txt'文件的情况下执行此操作?我实际上没有这个文件,因为我接收'commands.txt'作为流。 – dhokas

+0

@dhokas:引用我的更新 – Inian