Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-12-2004, 04:52 PM
|
#1
|
|
Member
Registered: Apr 2003
Posts: 178
Rep:
|
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 $?
Last edited by Linh; 05-12-2004 at 04:57 PM.
|
|
|
|
05-12-2004, 06:24 PM
|
#2
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
"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?
Last edited by Hko; 05-12-2004 at 06:26 PM.
|
|
|
|
05-13-2004, 09:27 AM
|
#3
|
|
Member
Registered: Apr 2003
Posts: 178
Original Poster
Rep:
|
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."
|
|
|
|
05-13-2004, 10:09 AM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
05-13-2004, 11:06 AM
|
#5
|
|
Member
Registered: Apr 2003
Posts: 178
Original Poster
Rep:
|
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 $?
|
|
|
|
05-13-2004, 01:21 PM
|
#6
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
echo $?
If you've coded C you know that you can call [ or
main() has 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.
|
|
|
|
05-13-2004, 01:46 PM
|
#7
|
|
Member
Registered: Apr 2003
Posts: 178
Original Poster
Rep:
|
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 $?
|
|
|
|
05-14-2004, 11:11 AM
|
#8
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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:
Or, if you want your script also to return the same code:
Code:
EXITCODE=$?
echo $EXITCODE
exit $EXITCODE
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:39 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|