如何将系统命令的结果存储在 Perl 变量中?

发布于 2024-09-26 03:37:31 字数 221 浏览 0 评论 0原文

$ cat test.pl
my $pid = 5892;
my $not = system("top -H -p $pid -n 1 | grep myprocess | wc -l");
print "not = $not\n";
$ perl test.pl
11
not = 0
$

我想将结果(即 11)捕获到变量中。我怎样才能做到这一点?

$ cat test.pl
my $pid = 5892;
my $not = system("top -H -p $pid -n 1 | grep myprocess | wc -l");
print "not = $not\n";
$ perl test.pl
11
not = 0
$

I want to capture the result i.e. 11 into a variable. How can I do that?

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

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

发布评论

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

评论(6

糖粟与秋泊 2024-10-03 03:37:32

来自 Perlfaq8

您混淆了 system()反引号 (``)。 system() 运行命令并返回退出状态信息(作为 16 位值:低 7 位是进程死亡的信号(如果有),高 8 位是实际退出值)。反引号 (``) 运行命令并将其发送到 STDOUT 的内容返回。

$exit_status   = system("mail-users");
$output_string = `ls`;

有多种方法可以从 Perl 执行外部命令。最常见的含义是:

  • system() :您想要执行命令但不想捕获其输出
  • exec :您不想返回
    调用perl脚本
  • 反引号:你想要捕获
    命令的输出
  • open您想要通过管道传输命令(作为
    输入或输出)到您的脚本

另请参阅 如何从外部命令捕获 STDERR?

From Perlfaq8:

You're confusing the purpose of system() and backticks (``). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.

$exit_status   = system("mail-users");
$output_string = `ls`;

There are many ways to execute external commands from Perl. The most commons with their meanings are:

  • system() : you want to execute a command and don't want to capture its output
  • exec: you don't want to return to the
    calling perl script
  • backticks : you want to capture the
    output of the command
  • open: you want to pipe the command (as
    input or output) to your script

Also see How can I capture STDERR from an external command?

中二柚 2024-10-03 03:37:32

最简单的方法是使用 Perl 中的 `` 功能。这将执行里面的内容并将打印的内容返回到标准输出:

 my $pid = 5892;
 my $var = `top -H -p $pid -n 1 | grep myprocess | wc -l`;
 print "not = $var\n";

这应该可以做到。

The easiest way is to use the `` feature in Perl. This will execute what is inside and return what was printed to stdout:

 my $pid = 5892;
 my $var = `top -H -p $pid -n 1 | grep myprocess | wc -l`;
 print "not = $var\n";

This should do it.

季末如歌 2024-10-03 03:37:32

尝试使用 qx{command} 而不是反引号。对我来说,它更好一点,因为:你可以用它执行 SQL,而不用担心转义引号之类的问题。根据编辑器和屏幕的不同,我的老眼睛往往会错过微小的反勾号,并且它不应该出现像使用尖括号与通配符那样的过载问题。

Try using qx{command} rather than backticks. To me, it's a bit better because: you can do SQL with it and not worry about escaping quotes and such. Depending on the editor and screen, my old eyes tend to miss the tiny back ticks, and it shouldn't ever have an issue with being overloaded like using angle brackets versus glob.

月牙弯弯 2024-10-03 03:37:32

使用 backtickqx 有帮助,感谢大家的回答。但是,我发现如果您使用 backtickqx,输出会包含尾随换行符,我需要将其删除。所以我使用了 chomp。

chomp($host = `hostname`);
chomp($domain = `domainname`);
$fqdn = $host.".".$domain;

更多信息请点击这里:
http://irouble.blogspot.in/2011/04/perl-chomp -反引号.html

Using backtick or qx helps, thanks everybody for the answers. However, I found that if you use backtick or qx, the output contains trailing newline and I need to remove that. So I used chomp.

chomp($host = `hostname`);
chomp($domain = `domainname`);
$fqdn = $host.".".$domain;

More information here:
http://irouble.blogspot.in/2011/04/perl-chomp-backticks.html

会傲 2024-10-03 03:37:32

对系统命令使用反引号,这有助于将其结果存储到 Perl 变量中。

my $pid = 5892;
my $not = ``top -H -p $pid -n 1 | grep myprocess | wc -l`; 
print "not = $not\n";

Use backticks for system commands, which helps to store their results into Perl variables.

my $pid = 5892;
my $not = ``top -H -p $pid -n 1 | grep myprocess | wc -l`; 
print "not = $not\n";
国际总奸 2024-10-03 03:37:32

也用于例如。您可以使用 IPC::Run

use IPC::Run qw(run);

my $pid = 5892;
run [qw(top -H -n 1 -p), $pid],
    '|', sub { print grep { /myprocess/ } <STDIN> },
    '|', [qw(wc -l)],
    '>', \my $out;

print $out;
  • 进程在没有 bash 的情况下运行 子进程
  • 可以通过管道传输到 perl subs
  • 与 shell 非常相似

Also for eg. you can use IPC::Run:

use IPC::Run qw(run);

my $pid = 5892;
run [qw(top -H -n 1 -p), $pid],
    '|', sub { print grep { /myprocess/ } <STDIN> },
    '|', [qw(wc -l)],
    '>', \my $out;

print $out;
  • processes are running without bash subprocess
  • can be piped to perl subs
  • very similar to shell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文