python - 如果不重写 Python/Perl 脚本,我如何在 bash 脚本中通过管道将输出连接在一起?

标签 python linux bash perl shell

我有以下 Perl 脚本(尽管这适用于 Python 和其他脚本语言):script1.plscript2.plscript3.pl

这些脚本的编写方式是,用户使用输入标志执行它们,输出是保存的文件。

perl script1.pl --i input1.tsv   ## this outputs the file `outputs1`
perl script2.pl --i outputs1     ## this outputs the file `outputs2`
perl script3.pl --i outputs2     ## this outputs the file `final_output`

(对于 Pythonistas,这是 python script1.py)

现在,我想创建一个可执行的 bash 脚本,允许用户简单地使用 input1 并获取返回的输出 final_output

以下是我如何仅使用一个 Perl 脚本 execute.sh 来完成此操作:

#!/bin/sh

source ~/.bash_profile

FLAG1="--i=$1"

perl script1.pl $FLAG1

可以在命令行上运行execute.sh input1.tsv

对于我使用三个脚本的示例,我如何将中间输出通过管道传输到中间脚本中以创建一个 execute.sh 脚本,例如outputs1script2.pl,然后 outputs2scripts3.pl,等等?

有没有办法让我在不重写 perl/python 脚本的情况下做到这一点?

编辑:附加信息:问题是我实际上不知道输出是什么。文件名根据原始的inputs1.tsv 进行更改。现在,我确实知道输出的文件扩展名。但outputs1和outputs2具有相同的文件扩展名。

最佳答案

此类情况的最佳实践是从 stdin 读取脚本并将其写入 stdout。在这种情况下,将它们通过管道连接在一起变得非常容易,如下所示:

perl script1.pl < input1.tsv | perl script2.pl | perl script3.pl

就您而言,您可以编写如下脚本:

#!/bin/sh
perl script1.pl --i input1.tsv
perl script2.pl --i outputs1 
perl script3.pl --i outputs2

这并不理想,但它会做你想要的。它将读取 input1.tsv,并写入output3。

关于python - 如果不重写 Python/Perl 脚本,我如何在 bash 脚本中通过管道将输出连接在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41952421/

相关文章:

python - Flask:多个蓝图相互干扰

python - 如何谈论比较字符串中字符的位置?

python - 广告扰乱了我的文章爬行

linux - centOS 7 屏幕分辨率 - HyperV 托管

Linux 庆典 : Combine two csv files with different headers

python 通过 ssh 连接 mysql

c - X509_NAME_get_text_by_NID 返回带有数字的 CN 作为文本

linux - cryptsetup 可以使用已知 key 列表进行离线攻击吗?

bash - Shell 脚本 "|| { }"可读替代

linux - bash:递归地将父目录中的所有匹配文件移动到新的子目录