Groups > Unix Linux > Linux > Re: Capturing program return codes in a bash script?




Re: Capturing program return codes in a bash script?

Re: Capturing program return codes in a bash script?
Thu, 03 Apr 2008 12:30:19 -040
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.          ------

Post Reply
Re: Capturing program return codes in a bash script?
Thu, 03 Apr 2008 13:35:18 +000
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
Post Reply
Re: Capturing program return codes in a bash script?
Thu, 03 Apr 2008 16:58:22 +020
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
Post Reply
about | contact