Computing.Net > Forums > Unix > force the script to exit w/0

force the script to exit w/0

Reply to Message Icon

Original Message
Name: Anu
Date: December 4, 2002 at 10:22:09 Pacific
Subject: force the script to exit w/0
OS: aix 4.33
CPU/Ram: powerpc/ 2GB
Comment:

I need scripting help. I have a unix Korns shell running sub-scripts that sometimes return with error. I want to capture the returncodes to a file and exit the parent script with returncode=0.


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: December 7, 2002 at 10:17:40 Pacific
Subject: force the script to exit w/0
Reply: (edit)

All you need to do is to explicitly exit with "exit 0" such as:

#!/bin/sh
subscript1.sh
exit 0

If you omit that exit, then the main script will exit with the exit code of the last command that it ran - in this case the exit code of subscript1.sh.

If you do something like:

#!/bin/sh
subscript1.sh
print "exit code of subscript1.sh was $?"

the main script will print the exit code of the sub-script, but it will then exit with a status of zero, which was the status of the successful print command.

If you want to test the status of a sub-script, then exit using that code, you need to capture it first:

#!/bin/sh
subscript1.sh
rc=$?
if [ $rc -eq 0 ] ; then
print "subscript1.sh was successful"
else
print "subscript1.sh was NOT successful"
fi
exit $rc


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: force the script  to exit w/0 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge