Search code examples
perlipc

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


perl -e 'system ("crontab1 -l");print $?'

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

perl -e 'system ("crontab1 -l|grep blah");print $?'

returns 256.

What is the way to check the status of the first (or both) programs?


Solution

  • You are getting the exit status of the entire command, just as you should. If you want the exit status separately, you're going to have to run the commands separately.

    #!/usr/bin/perl -e
    system("crontab1 -l > /tmp/junk.txt"); print $?;
    system("grep blah /tmp/junk.txt"); print $?;
    

    as an example.