Technology Questions

Go Back   Technology Questions > Software Questions > Operating System Questions > Linux



Reply
 
LinkBack Thread Tools
  #1  
Old 04-02-2008, 02:30 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Capturing program return codes in a bash script?


How do I determine if an command completed successfully when run from a
bash script to decide what the script should do next?

Working on a mysqldump backup script which works pretty nice... but
doesn't really care whether the database dump dies, errors out or
completes successfully.

I figure that's kind of important for a backup script.

--
"Remain calm, we're here to protect you!"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 04-02-2008, 02:30 PM
  #2  
Old 04-02-2008, 02:40 PM
Bit Twister
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote:
>
> How do I determine if an command completed successfully when run from a
> bash script to decide what the script should do next?


Depends on the application.
Usually return code is in $?

Example:
ping -c 1 -w 3 $_target_host > /dev/null
if [ $? -eq 0 ] ; then
echo "Ping failure on $_target_host"
exit 1
fi

On some apps I have had to redirect error output to a file
and use grep to check for error string.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3  
Old 04-02-2008, 02:50 PM
Bit Twister
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

On Wed, 2 Apr 2008 21:30:42 +0000 (UTC), Bit Twister wrote:
> On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote:
>>
>> How do I determine if an command completed successfully when run from a
>> bash script to decide what the script should do next?

>
> Depends on the application.
> Usually return code is in $?
>
> Example:
> ping -c 1 -w 3 $_target_host > /dev/null
> if [ $? -eq 0 ] ; then


Oops. that should read
if [ $? -ne 0 ] ; then
echo "Ping failure on $_target_host"
exit 1
fi

For extra points read
man test
http://tldp.org/LDP/abs/html/index.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4  
Old 04-02-2008, 02:50 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

On Wed, 02 Apr 2008 21:30:42 +0000, Bit Twister wrote:

> On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote:
>>
>> How do I determine if an command completed successfully when run from a
>> bash script to decide what the script should do next?

>
> Depends on the application.
> Usually return code is in $?
>
> Example:
> ping -c 1 -w 3 $_target_host > /dev/null if [ $? -eq 0 ] ; then
> echo "Ping failure on $_target_host" exit 1
> fi
>
> On some apps I have had to redirect error output to a file and use grep
> to check for error string.


Grepping through an output file is how I have been doing it... which is
prone to not being able to plan for the unknown.

I figured there had to be a more standardized way of doing it.

So I guess I'd have to test the application to see if it produces the $?
and adjust accordingly.

--
"Remain calm, we're here to protect you!"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5  
Old 04-02-2008, 02:50 PM
Unruh
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

Ivan Marsh <annoyed@you.now> writes:


>How do I determine if an command completed successfully when run from a
>bash script to decide what the script should do next?


>Working on a mysqldump backup script which works pretty nice... but
>doesn't really care whether the database dump dies, errors out or
>completes successfully.


$?
is the return code of the last command run.

command1 && command2
executes command2 only if command1 completes suddessfully

if command; then
command1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #6  
Old 04-02-2008, 03:00 PM
Bit Twister
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

On Wed, 02 Apr 2008 16:43:08 -0500, Ivan Marsh wrote:
>
> Grepping through an output file is how I have been doing it... which is
> prone to not being able to plan for the unknown.
>
> I figured there had to be a more standardized way of doing it.
>
> So I guess I'd have to test the application to see if it produces the $?
> and adjust accordingly.


Just repeating the warning, I have had a good return code in $? and a
failure in standard error. :(
It just depends on the application.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7  
Old 04-02-2008, 03:20 PM
John Hasler
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

Bit Twister writes:
> Just repeating the warning, I have had a good return code in $? and a
> failure in standard error.


That's a bug.
--
John Hasler
john@dhh.gt.org
Dancing Horse Hill
Elmwood, WI USA
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8  
Old 04-02-2008, 09:50 PM
s. keeling
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

John Hasler <john@dhh.gt.org>:
> Bit Twister writes:
> > Just repeating the warning, I have had a good return code in $? and a
> > failure in standard error.

>
> That's a bug.


Or he's mis-read the manpage's description of return values.

Not everything says "$? == 0" on success. Which process in a chain
does he want to consider the definitive result?

foo > blah | huggghh! ...

He might fiddle with wrapping parts in parens (run in subshells).


--
Any technology distinguishable from magic is insufficiently advanced.
(*) http://blinkynet.net/comp/uip5.html Linux Counter #80292
- - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #9  
Old 04-03-2008, 06:40 AM
Chris F.A. Johnson
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

On 2008-04-02, Ivan Marsh wrote:
>
> How do I determine if an command completed successfully when run from a
> bash script to decide what the script should do next?
>
> Working on a mysqldump backup script which works pretty nice... but
> doesn't really care whether the database dump dies, errors out or
> completes successfully.
>
> I figure that's kind of important for a backup script.


if command args ## replace with your command
then
echo success
else
echo failure
fi

Or:

command args
if [ $0 -eq 0 ]
then
echo success
else
echo failure
fi



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #10  
Old 04-03-2008, 08:10 AM
Benny Nielsen
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

Chris F.A. Johnson wrote:

> On 2008-04-02, Ivan Marsh wrote:
>>
>> How do I determine if an command completed successfully when run from a
>> bash script to decide what the script should do next?
>>


Normally you get $? as the status for the last command. Most programs uses 0
as failure and 1 as success. See man bash on status is set in different
situations like pipes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #11  
Old 04-03-2008, 09:41 AM
Lew Pitcher
Newsgroup Contributor
 
Posts: n/a
Re: Capturing program return codes in a bash script?

Benny Nielsen wrote:

> Chris F.A. Johnson wrote:
>
>> On 2008-04-02, Ivan Marsh wrote:
>>>
>>> How do I determine if an command completed successfully when run from a
>>> bash script to decide what the script should do next?
>>>

>
> Normally you get $? as the status for the last command. Most programs uses
> 0 as failure and 1 as success.


Nope. Most programs use a return code of 0 to indicate success, and a
non-zero value to indicate the opposite (not always failure).

> See man bash on status is set in different
> situations like pipes.


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
change directory in a bash script Adrian Schmitt Linux 3 02-24-2008 12:10 PM
grub in bash script? confuzious Linux 5 10-22-2007 09:50 AM
Bash script glitch Mike Linux 6 08-11-2007 05:30 AM
Bash script questions. Longfellow Linux 5 05-06-2007 12:26 AM
grep in a bash script hobbzilla Linux 4 05-06-2007 12:11 AM


New To Technology Questions? Do You Need Help with Your Computer or Device? Do You Need Help with this site?

All times are GMT -8. The time now is 10:06 AM.


2003 - 2010 All Rights Reserved. Technology Questions

Search Engine Friendly URLs by vBSEO 3.3.0