How can I check the status of the first program in pipeline in Perl's system()?

后端 未结 7 634
小蘑菇
小蘑菇 2021-01-21 06:41
perl -e \'system (\"crontab1 -l\");print $?\'

returns -1 as expected (program crontab1 doesn\'t exist)

perl -e \'system (\"crontab1 -l|         


        
7条回答
  •  情书的邮戳
    2021-01-21 07:26

    Remember, you're supposed to use $?>>8 to get the exit code, not $?

    perl -e 'system("set -o pipefail;false | true");print $?>>8,"\n"'
    

    1

    This ("pipefail") only works if your shell is bash 3. Cygwin and linux ship with it; not sure about mac.

    You should be aware that 256 is an error return. 0 is what you'd get on success:

    perl -e 'system("true");print $?>>8,"\n"'
    

    0

    I didn't know system returned -1 for a single command that isn't found, but $?>>8 should still be non-zero in that case.

提交回复
热议问题