I have开发者_StackOverflow社区 a perl script (verifyCopy.pl
) that uses system()
to call a shell script (intercp.sh
).
From inside the shell script, I have set up several exit's with specific exit codes and I'd like to be able to do different things based on which exit code is returned.
I've tried using $?
, I have tried assigning the value of system("./intercp.sh")
to a variable then checking the value of that, but the error message is always 0.
Is this because even though something inside the shell script fails, the actual script succeeds in running?
I tried adding a trap in the shell script (ie trap testexit EXIT
and testexit() { exit 222; }
but that didn't work either.
$?
should catch the exit code from your shell script.
$ cat /tmp/test.sh
#!/bin/sh
exit 2
$ perl -E 'system("/tmp/test.sh"); say $?'
512
Remember that $?
is encoded in the traditional manner, so $? >> 8
gives the exit code, $? & 0x7F
gives the signal, and $? & 0x80
is true if core was dumped. See perlvar for details.
Your problem may be one of several things: maybe your shell script isn't actually exiting with the exit code (maybe you want set -e
); maybe you have a signal handle for SIGCHLD eating the exit code; etc. Try testing with the extremely simple shell script above to see if its a problem in your perl script or your shell script.
精彩评论