Returning exit status to calling script

Returning exit status to calling script

Post by Steve Hazelet » Fri, 27 Nov 1998 04:00:00



I have a bash script that calls another script.  There it does some things and
if there is a problem I then exit with exit 1 otherwise I exit 0.  I would
like to know how to pass this back to the original script so that I can use
the case statements to determine what happened.  Can anyone post an example or
show me a URL that has some examples.
TIA

... Steve

--

 
 
 

Returning exit status to calling script

Post by Niall Smar » Sat, 28 Nov 1998 04:00:00



> I have a bash script that calls another script.  There it does some things and
> if there is a problem I then exit with exit 1 otherwise I exit 0.  I would
> like to know how to pass this back to the original script so that I can use
> the case statements to determine what happened.

The shell variable $? stores the return code of the most recently executed
command.  For example:

[shake ~]$ (exit 2)
[shake ~]$ echo $?
2
[shake ~]$ (exit 0)
[shake ~]$ echo $?
0

You need to read a tutorial on shell scripts:

http://www.oase-shareware.org/shell/links.html

Regards,

Niall

 
 
 

Returning exit status to calling script

Post by James Carte » Sun, 29 Nov 1998 04:00:00


Steve,

The $? variable should do what you want.  It contains the exit status of
the most recently executed command:

#!/bin/bash
/usr/local/bin/other_script
STATUS=$?
case ${STATUS} in
        0)      echo other_script ran without errors
                ;;
        1)      echo Error: other_script exited with status ${STATUS}
                ;;
        *)      echo Error: other_script exited with unknown status ${STATUS}
                ;;
esac


> I have a bash script that calls another script.  There it does some things and
> if there is a problem I then exit with exit 1 otherwise I exit 0.  I would
> like to know how to pass this back to the original script so that I can use
> the case statements to determine what happened.  Can anyone post an example or
> show me a URL that has some examples.

 
 
 

Returning exit status to calling script

Post by Michael Wa » Sun, 29 Nov 1998 04:00:00



Quote:>/usr/local/bin/other_script
>STATUS=$?
>case ${STATUS} in
>    0)      echo other_script ran without errors
>            ;;
>    1)      echo Error: other_script exited with status ${STATUS}
>            ;;
>    *)      echo Error: other_script exited with unknown status ${STATUS}
>            ;;
>esac

/usr/local/bin/other_script ran with status 0 does not necessarily
/usr/local/bin/other_script ran without errors.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

Returning exit status to calling script

Post by James Carte » Mon, 30 Nov 1998 04:00:00


[ ... ]

Quote:> /usr/local/bin/other_script ran with status 0 does not necessarily
> /usr/local/bin/other_script ran without errors.

Steve specified in his posting that the other_script would exit with a 1
if it encountered problems, or with a 0 otherwise:


> I have a bash script that calls another script.  There it does some things and
> if there is a problem I then exit with exit 1 otherwise I exit 0.  I would
> like to know how to pass this back to the original script so that I can use
> the case statements to determine what happened.  Can anyone post an example or
> show me a URL that has some examples.

Regards,
James
 
 
 

Returning exit status to calling script

Post by Icarus Spar » Tue, 01 Dec 1998 04:00:00



Quote:>I have a bash script that calls another script.  There it does some things and
>if there is a problem I then exit with exit 1 otherwise I exit 0.  I would
>like to know how to pass this back to the original script so that I can use
>the case statements to determine what happened.  Can anyone post an example or
>show me a URL that has some examples.
>TIA

Just use

if otherscript with its args
then
        echo 'All was OK'
else
        echo 'otherscript had a problem'
fi

You do not need '$?' or 'case' to solve this problem.
Icarus