【问题标题】:execute a list of commands and store outputs into a list执行命令列表并将输出存储到列表中
【发布时间】:2017-04-25 09:40:18
【问题描述】:

我收到一个命令流作为输入:

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

【问题讨论】:

  • 您能说明如何使用parallel 解决问题吗?
  • 当然,我更新了。
  • 你有两个单独的文件中的命令和输出吗?
  • 是的,我将输出放在一个文件中,然后创建一个包含命令和输出的文件,但我想直接创建最后一个文件。

标签: bash parallel-processing pipe output


【解决方案1】:

只是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

【讨论】:

  • 谢谢。是否有等价物可以让我在不必创建commands.txt 文件的情况下执行此操作?我实际上没有这个文件,因为我收到commands.txt 作为流。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 2020-01-02
相关资源
最近更新 更多