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

2024-09-26 18:08:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下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`

(对于Python,这是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具有相同的文件扩展名。你知道吗


Tags: 文件the脚本tsvshthisoutputsperl
2条回答

对于这种情况的最佳实践是从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。你知道吗

您的问题不是这样说的,但是假设您可以使用 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。你知道吗

相关问题 更多 >

    热门问题