如何在 Perl 脚本中调用 shell 命令?

2024-04-21

我如何调用 shell 命令的示例是什么,比如'ls -a' 在 Perl 脚本中以及检索命令输出的方法?


如何从 Perl 程序运行 shell 脚本 http://www.perlmonks.org/?node_id=78523

1. 使用系统system($command, @arguments);

例如:

system("sh", "script.sh", "--help" );

system("sh script.sh --help");

系统将执行 $command @arguments 并在完成后返回到您的脚本。你可以检查$! 对于外部应用程序传递给操作系统的某些错误。读 系统文档,了解各种差异的细微差别 调用略有不同。

2. 使用exec

这与使用非常相似system http://perldoc.perl.org/functions/system.html,但它会 执行后终止脚本。再次,阅读文档 为了exec http://perldoc.perl.org/functions/exec.html了解更多。

3. 使用反引号或qx//

my $output = `script.sh --option`;

my $output = qx/script.sh --option/;

反引号运算符及其等效项qx//,执行操作符内的命令和选项,并在完成时将命令输出返回到 STDOUT。

还有一些方法可以通过创造性地使用来运行外部应用程序open http://perldoc.perl.org/functions/open.html,但这是高级用法;阅读文档了解更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Perl 脚本中调用 shell 命令? 的相关文章

随机推荐