简体   繁体   English

Perl脚本系列。 BASH,BATCH,Shell?

[英]Series of Perl Scripts. BASH, BATCH, Shell?

I have a series of perl scripts that I want to run one after another on a unix system. 我有一系列perl脚本,我想在unix系统上一个接一个地运行。 What type of file would this be / could I reference it as in documentation? 这将是什么类型的文件/我可以像在文档中一样引用它吗? BASH, BATCH, Shell Script File? BASH,BATCH,Shell脚本文件?

Any help would be appreciated. 任何帮助,将不胜感激。

Simply put the commands you would use to run them manually in a file (say, perlScripts.sh ): 只需将用于手动运行它们的命令放在一个文件中(例如, perlScripts.sh ):

#!/bin/sh

perl script1.pl
perl script2.pl
perl script3.pl

Then from the command line: 然后从命令行:

$ sh perlScripts.sh

Consider using Perl itself to run all of the scripts. 考虑使用Perl本身来运行所有脚本。 If the scripts don't take command line arguments, you can simply use: 如果脚本不带命令行参数,则可以简单地使用:

do 'script1.pl';
do 'script2.pl';

etc. 等等

do 'file_name' basically copies the file's code into the current script and executes it . do 'file_name'基本上将文件的代码复制到当前脚本中并执行它 It gives each file its own scope, however, so variables won't clash. 但是,它为每个文件提供了自己的作用域,因此变量不会发生冲突。

This approach is more efficient, because it starts only one instance of the Perl interpreter. 这种方法效率更高,因为它仅启动Perl解释器的一个实例。 It will also avoid repeated loading of modules. 它还将避免重复加载模块。

If you do need to pass arguments or capture the output, you can still do it in a Perl file with backquotes or system : 如果确实需要传递参数或捕获输出,仍然可以在带有反引号或system的Perl文件中进行操作:

my $output = `script3.pl file1.txt`; #If the output is needed.

system("script3.pl","file1.txt");    #If the output is not needed.

This is similar to using a shell script. 这类似于使用shell脚本。 However, it is cross-platform compatible. 但是,它是跨平台兼容的。 It means your scripts only rely on Perl being present, and no other external programs. 这意味着您的脚本仅依赖于Perl的存在,而没有其他外部程序。 And it allows you to easily add functionality to the calling script. 它使您可以轻松地向调用脚本添加功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM