LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash command $? failed to execute. (http://www.linuxquestions.org/questions/programming-9/bash-command-%24-failed-to-execute-180817/)

Linh 05-12-2004 04:52 PM

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 $?

Hko 05-12-2004 06:24 PM

"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?

Linh 05-13-2004 09:27 AM

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."

Hko 05-13-2004 10:09 AM

Re: reply to Hko
 
Quote:

Originally posted by Linh
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."

Exactly. This does not mean, it will execute it another time. It's just the return value / exit code of a command already executed.

Linh 05-13-2004 11:06 AM

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 $?

jim mcnamara 05-13-2004 01:21 PM

echo $?

If you've coded C you know that you can call [
Code:

exit(1)
or
main() has
Code:

return 0;
The are return codes - the status of the process or file when it exited.
Generally 0 means good, any other number means error.

Code:

cat /usr/include/sysexits.h
shows you how exit codes are used. 128 - 172 are "reserved" for the system or shell, for example. Most code just uses a 1 for every error, which is not very informative.

Linh 05-13-2004 01:46 PM

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 $?

Hko 05-14-2004 11:11 AM

Re: reply to Hko
 
Quote:

How would I print out the exit code returned to the shell ? Would it be something like echo exit $? [/B]
Easy:
Code:

echo $?
Or, if you want your script also to return the same code:
Code:

EXITCODE=$?
echo $EXITCODE
exit $EXITCODE



All times are GMT -5. The time now is 09:13 AM.