如果不重写Python / Perl脚本,我将如何在bash脚本中管道输出?

问题描述 投票:-2回答:2

我有以下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脚本,例如outputs1进入script2.pl,然后outputs2进入scripts3.pl等?

有没有办法在不重写perl / python脚本的情况下执行此操作?

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

python linux bash perl shell
2个回答
0
投票

这种情况的最佳实践是从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,并写出outputs3。


-1
投票

您的问题没有说明,但假设您可以使用--o标志指定输出文件:

perl script1.pl --i input1.tsv --o /dev/stdout | perl script2.pl --i /dev/stdin --o /dev/stdout | perl script3.pl --i /dev/stdin --o final_output

/dev/stdin/dev/stdout是神奇的unix文件,分别写入进程'stdinstdout

© www.soinside.com 2019 - 2024. All rights reserved.