gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:04:03 26 4
gpt4 key购买 nike

如何调用 shell 命令(例如 Perl 脚本中的“ls -a”)以及检索命令输出的方法的示例是什么?

最佳答案

How to run a shell script from a Perl program

1. Using system system($command, @arguments);

For example:

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

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

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. Using 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. Using backticks or qx//

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

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

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.

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

关于perl - 如何在 Perl 脚本中调用 shell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com