我有以下Perl脚本(尽管这适用于Python和其他脚本语言):script1.pl
、script2.pl
、script3.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脚本的情况下做到这一点?
编辑:附加信息:问题是我实际上不知道输出是什么。文件名根据原始输入1.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,并写入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文件,分别写入进程的stdin
和stdout
。