Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

Unix Exit Codes

Hi,

I'm using a script to start my application on a daily basis. A cron-like application called Control-M calls this script at a give time and starts the app. The script istelf is very simple as you can see below.

However the line "Load app" occasionally has thrown an error which has resulted in "app.sh" failing to start up the applicartion. I need to check the "Load app" command has completed successfully before calling app.sh, otherwise abort.

How can I do this? Some one mention using exit codes, but I don't know how this works. any ideas?
#!/bin/ksh
. /apps/bin/0LOAD
Load app
app.sh

Open in new window

Avatar of wilhelm_voigt
wilhelm_voigt
Flag of Austria image

It's covered here: http://www.bolthole.com/solaris/ksh-basics.html

The exit status is available in the variable $? directly after the call. So after line 3, you can include a block like the one described in the link above:
if [ $? -eq 0 ] ; then
	print we are okay
else
	print something failed
fi

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Couldnt you also do

#!/bin/ksh
. /apps/bin/0LOAD
Load app && app.sh

Open in new window

or
#!/bin/ksh
. /apps/bin/0LOAD
Load app
if [[ $? = 0 ]]
then
   app.sh
else
   echo "Load app failed with error $?"
fi

Open in new window

Avatar of bowemc
bowemc

ASKER

what does Load app && app.sh do? I don't understand &&
I think.... from a somewhat failing memory...

Load app runs, if it runs successfully (ie return code 0) it will then run app.sh
If load app returns anything other than return code 0 app.sh will not run, this should be ok in most cases but if the Load app returns a valid (perhaps for a warning) non zero return code then it will not run app.sh in any case. But I guess that will be true of all the examples above anyway...

You may want to use something like the 2nd option in the "or" example, you may find it easier to read and debug if you have to.
What does the error look like that's thrown by `Load App`? In some circumstances it might even be that this command throws an error but quits with exit code 0 (I saw that often with old or badly written software), that way you won't be able to track the error in that application.
If it produces output to the screen, you might need to `grep` the output and act accordingly, f.ex. if the `Load App` invocation prints out "Error 0x8cf" when it fails, a script like this might help:
#!/bin/ksh
. /apps/bin/0LOAD
if ! Load app 2>&1 | grep -q "Error 0x8cf"; then
  app.sh
fi

Open in new window

&& is AND
|| is OR

So

cmd1 && cmd2

is a shortcut for

cmd1

if [ $? -eq 0 ]
then
    cmd1
fi

cmd1 || cmd2

is a shortcut for

cmd1

if [ $? -ne 0 ]
then
    cmd2
fi