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

前端 未结 5 795
醉梦人生
醉梦人生 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:54

    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

提交回复
热议问题