如何在 Perl 脚本中调用 shell 命令? [英] How can I call a shell command in my Perl script?

查看:71
本文介绍了如何在 Perl 脚本中调用 shell 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What would be an example of how I can call a shell command, say 'ls -a' in a Perl script and the way to retrieve the output of the command as well?

推荐答案

如何从一个 Perl 程序

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

例如:

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

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

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

System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application. Read the documentation for system for the nuances of how various invocations are slightly different.

2.使用 exec

这与system的使用非常相似,但它会在执行时终止您的脚本.再次阅读文档exec 了解更多.

This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more.

3.使用反引号或 qx//

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

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

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

The backtick operator and it's equivalent qx//, excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

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

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

这篇关于如何在 Perl 脚本中调用 shell 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆