LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-14-2006, 10:48 PM   #1
Jeiku
Member
 
Registered: Jul 2005
Posts: 64

Rep: Reputation: 18
Bash scripting problem with exit codes


Hi,

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

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

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

Code:
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!

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

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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:
Code:
#!/bin/bash

perl ./udp_weather2.pl
script_exit_value=$?
echo "exit code: ${script_exit_value} [AFTER]"

if [ "${script_exit_value}" -ne "0" ] ; then
        echo "running again..."
        perl ./udp_weather2.pl
else
        echo "exit code is ${?}. if that's one wtf is the script doing???"
fi
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
Jeiku
Member
 
Registered: Jul 2005
Posts: 64

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


Reply

Tags
bash, exit status



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 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
Open Source Consulting | Domain Registration