Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-14-2006, 10:48 PM   #1
Registered: Jul 2005
Posts: 64

Rep: Reputation: 18
Bash scripting problem with exit codes


I'm having some problems with Bash shell scripting, here is my script:

echo "exit code: ${?} [BEFORE]"
perl ./
echo "exit code: ${?} [AFTER]"

if [ "${?}" -ne "0" ] ; then
        echo "running again..."
        perl ./
        echo "exit code is ${?}. if that's one wtf is the script doing???"
The output is:

exit code: 0 [BEFORE]
time: 11:29
temperature: 0
radiation: 0
Null temp and rad values, exitting...
exit code: 1 [AFTER]
exit code is 1. if that's one wtf is the script doing???
Basically my if function is not catching the exit code of my perl script, even though the exit code is 1, it doesn't run the script again. Why is this?
My bash version:
GNU bash, version 3.00.16(2)-release (i486-slackware-linux-gnu)

Thanks for any help and I'm stumped on this one!

Old 05-14-2006, 11:46 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Well, there are two things I'd like to mention.

The $? variable is updated after every command. So, first it reflects the exit value from the perl script. Then, after the echo, $? is the exit value of the echo command. The if-statement references the echo command's exit value. To save it, you'll need to do something like this:

perl ./
echo "exit code: ${script_exit_value} [AFTER]"

if [ "${script_exit_value}" -ne "0" ] ; then
        echo "running again..."
        perl ./
        echo "exit code is ${?}. if that's one wtf is the script doing???"
The other thing is that the if-statement uses a numeric comparison (-ne) but uses string arguments. It might work, but it's a little unusual.
1 members found this post helpful.
Old 05-15-2006, 01:22 AM   #3
Registered: Jul 2005
Posts: 64

Original Poster
Rep: Reputation: 18
Cool, thanks! I didn't realise that $? is updated after every command... doh!


bash, exit status

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
BASH scripting problem danreed007 Programming 5 04-24-2006 05:02 AM
exit codes introuble Programming 2 05-08-2005 12:53 PM
bash scripting problem sutley Linux - Software 1 12-17-2004 11:33 AM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM

All times are GMT -5. The time now is 10:59 AM.

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