How can I store the result of a system command in a Perl variable?

前端 未结 5 797
醉梦人生
醉梦人生 2020-11-29 21:05
$ 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
$
5条回答
  •  不知归路
    2020-11-29 21:36

    Use backtick for system commands which helps to store their result into Perl variable.

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

提交回复
热议问题