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?
Wed, 02 Apr 2008 16:43:08 -050
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!"
Post Reply
Re: Capturing program return codes in a bash script?
Wed, 2 Apr 2008 21:30:42 +0000
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.
Post Reply
about | contact