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

前端 未结 5 796
醉梦人生
醉梦人生 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 22:01

    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.

提交回复
热议问题