LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 05-12-2004, 05:52 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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 05:57 PM.
 
Old 05-12-2004, 07:24 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
"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 07:26 PM.
 
Old 05-13-2004, 10:27 AM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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."
 
Old 05-13-2004, 11:09 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 05-13-2004, 12:06 PM   #5
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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 $?
 
Old 05-13-2004, 02:21 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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.
 
Old 05-13-2004, 02:46 PM   #7
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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 $?
 
Old 05-14-2004, 12:11 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind9: NDC command failed : rndc: connect failed: connection refused Boudewijn Linux - Networking 19 01-02-2014 08:19 AM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 07:31 AM
how to execute bash scripts from C????? :( cho Programming 2 06-19-2005 08:15 PM
Firefox failed to execute bachew Linux - Software 3 02-20-2005 10:13 AM
Failed to execute cgi script in Apache!! baby_linu Linux - Newbie 4 06-30-2004 05:23 PM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration