Getting STDOUT, STDERR, and response code from external *nix command in perl

后端 未结 4 1526
别跟我提以往
别跟我提以往 2020-11-28 16:26

I want to execute an external command from within my Perl script, putting the output of both stdout and stderr into a $variable of my choice, and to get the com

4条回答
  •  星月不相逢
    2020-11-28 17:09

    Actually, the proper way to write this is:

    #!/usr/bin/perl
    $cmd = 'lsss';  
    my $out=qx($cmd 2>&1);
    my $r_c=$?;
    print "output was $out\n";
    print "return code = ", $r_c, "\n";
    

    You will get a '0' if no error and '-1' if error.

提交回复
热议问题