Bash command $? failed to execute.
Bash command $? failed to execute.
The command $? means return value of the last command executed. Being the case, the script below should display a number 2 twice on the console because of the exit $? command. The number 2 only displayed once. ============================= #!/bin/bash -p n=2 echo $n exit $? |
"return value" is not the same as "output".
The return value (better: exit code" is an 'invisible' number passed to the shell from a program that ends. Often this is used to indicate the program ecountered an error: 0 when the program exited normally, and some other number (1..128) if there was an error. But your script is also a program, so it also returns an exit code (or: return value). You can do: exit 1 exit 100 exit 0 In your script, echo $n will probably run fine, and thus returns an exit code of 0 to your script. Then you $? will be "0". So your script returns exit code 0 to the schell where your script was started from. This can be useful: to return the same exit code as the last command before "exit" returned, But from your post I understand this was not what you expected. "exit $?" does not run the last command again. Where did you get this idea from? Novell login scripts maybe? |
reply to Hko
Hi Hko. Thank you for your explanation.
You asked "exit $?" does not run the last command again. Where did you get this idea from? Novell login scripts maybe?" On page 136 of the book "The UNIX Programming Environment" by Brian W. Kernighan and Bob Pike, it states that "$? - return value of the last command executed." |
Re: reply to Hko
Quote:
|
reply to Hko
Hi again Hko.
By using the script exit $? how would I know what code it returned to shell ? How would I print out the exit code returned to the shell ? Would it be something like echo exit $? |
echo $?
If you've coded C you know that you can call [ Code:
exit(1) main() has Code:
return 0; Generally 0 means good, any other number means error. Code:
cat /usr/include/sysexits.h |
jim mcnamara
Hi jim mcnamara. Thank you for your help.
When I used the Bash command exit $? as shown below, it did not print out a 0. It did echo a 2, so there is no error, but it did not print out a 0 on the console. How do I get exit $? to print a number returned to the shell on the console ? ======================= #!/bin/bash -p n=2 echo $n exit $? ====================== In the code below, I issued an illegal command "aaa", but it did print a returned error code on the console. How do I know what error number was returned to the shell ? root:/home/usr-suid-apache# ./a ./a: aaa: command not found #!/bin/bash -p aaa exit $? |
Re: reply to Hko
Quote:
Code:
echo $? Code:
EXITCODE=$? |
All times are GMT -5. The time now is 10:27 AM. |