像 exec() 这样的东西在 Perl 中返回一个值?

发布于 2024-12-11 15:37:28 字数 114 浏览 2 评论 0原文

我的印象(也许是错误的)Perl 中的“exec”函数不返回值(我的印象是它只是运行命令)。情况是 Perl 脚本正在服务器上运行,我需要调用该脚本在 Linux 机器内运行命令,同时还要返回结果。这怎么能做到呢?

I am under the impression (perhaps wrongly) that the 'exec' function in Perl does not return a value (I get the impression it just runs the command). The situation is that a Perl script is running on a server and I need to invoke this script to run commands inside the Linux box, but also return the results. How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ぃ弥猫深巷。 2024-12-18 15:37:28

如果“返回结果”是指返回命令的 STDOUT,那么您需要的是反引号或 qx()。例如:

my $result = qx(echo foo);
# or
my $result2 = `echo foo`;

请注意,不会返回发送至 STDERR 的消息。

如果您指的是程序的退出状态,请使用系统:

my $status = system("echo foo");
# or
my $status2 = system("/bin/echo", "foo", "bar");

If by "return the results" you mean return STDOUT of the commands, what you need is backticks or qx(). E.g.:

my $result = qx(echo foo);
# or
my $result2 = `echo foo`;

Do note that messages to STDERR are not returned.

If you mean the exit status of the program, use system:

my $status = system("echo foo");
# or
my $status2 = system("/bin/echo", "foo", "bar");
熊抱啵儿 2024-12-18 15:37:28

exec() 不仅不返回值,而且根本不返回。

perldoc -f exec

exec函数执行系统命令并且永不返回
如果您希望它返回,请使用 system 而不是 exec。

但我很确定你不想要 system() 的返回值,
您似乎想要命令的输出,所以:

perldoc -f 系统

这不是您想要用来捕获的内容
命令的输出,为此您应该仅使用反引号或
qx//,如 perlop/"STRING" 中所述。

Not only does exec() not return a value, it does not return at all.

perldoc -f exec

The exec function executes a system command and never returns
use system instead of exec if you want it to return.

But I'm pretty sure that you do NOT want the return value of system(),
you seem to want the output of the command, so:

perldoc -f system

This is not what you want to use to capture
the output from a command, for that you should use merely backticks or
qx//, as described in perlop/"STRING".

骄兵必败 2024-12-18 15:37:28

运行程序并保存其结果的常用 Perl 方法是 反引号< /a>:

my $foo = `ls`;

The usual perl way to run a program and save its results is backticks:

my $foo = `ls`;
平定天下 2024-12-18 15:37:28

来自文档:http://perldoc.perl.org/functions/system.html

我一般用这个:

if(system(@args) != 0)
{
if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
            ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }
}

From the documentation : http://perldoc.perl.org/functions/system.html

I generally use this :

if(system(@args) != 0)
{
if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
            ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }
}
辞旧 2024-12-18 15:37:28

捕获 STDOUT:

my $dir = `pwd`;
my $dir = qx/pwd/;

捕获返回状态:

my $rc = system('pwd');

Perldocs 捕获 STDERR 和 STDOUT

my $output = `cmd 2>&1`;

< em>请参阅链接以了解捕获一个输出流的其他方法,但不能捕获另一个输出流

Capture STDOUT:

my $dir = `pwd`;
my $dir = qx/pwd/;

Capture Return Status:

my $rc = system('pwd');

Perldocs Capture STDERR and STDOUT:

my $output = `cmd 2>&1`;

See the link for other ways to capture one, output stream, but not the other

草莓酥 2024-12-18 15:37:28

在 perl 中使用 back-tic

my $var = `shell command`;

允许您执行 shell 命令,并且它返回任何输出。

using the back-tic

my $var = `shell command`;

in perl allows you to execute shell commands and it returns whatever the output was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文