Unix return code example


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix return code example
# 1  
Old 09-27-2011
Power Unix return code example

Hi,

Does anyone here can guide me to understand how is return code works in a parent-child relation with a simple example?
I have a request to build the script with return code in a child script, but i want to understand how does child script can return a code to the parent, stated if its succeeded or failed (know that 0 is success, anything more than 0 is failed).
Thanks for any advise given.
Appreciate help in advance.
best regards
# 2  
Old 09-27-2011
A script sets the return value using the exit command. This small script exits with a success (0) if the file named on the command line is found:

Code:
#!/usr/bin/env ksh
if [[ -f ${1:-no-such-file} ]]
then
   exit 0
else
   exit 1
fi

If no file is given on the command line, or the file isn't found it returns 'bad.'
# 3  
Old 09-28-2011
Since the OP was asking about exit status's in regards to child processes... I thought I would add this.


Consider two shell scripts, parent and child:

PARENT (test-parent.sh):
Code:
#!/bin/sh


# bash and ksh will expand '$?' to be the exit status of the previous command
# we can use that to get a child scripts exit status
# If you have many child processes and they are running
# in parallel you will want to look into the trap statement.
# trap will allow you to get info back from the child using something like
# trap "command to run on trap" CHLD
# where CHLD is the exit status of the child process

echo "I am the parent script."
echo
echo "I am now calling the child script..."
echo
echo "###############################################"
./test-child.sh
XSTAT=$?
echo "###############################################"
echo
echo "We have now obtained the exit status of the child (exit status: ${XSTAT})."
echo "We can determine what to do (exit with bad exit code or live on)."
echo
if [ "${XSTAT}" -gt 0 ]; then
        echo "Bad exit from child... exiting."
        exit ${XSTAT}
else
        echo "The child exited gracefully with exit status '${XSTAT}'"
        echo "We can now proceed doing other tasks as usual."
fi
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Weeeeeee other tasks."
exit ${XSTAT}


CHILD(test-child.sh):
Code:
#!/bin/sh

echo "I am the child script."
echo -n "What would you like my exit status to be: "
read XSTAT
echo "Exiting with exit status: ${XSTAT}"; exit ${XSTAT}


Now see the output when run...

BAD EXIT STATUS (not 0)
Code:
[user@host ~]$ ./test-parent.sh 
I am the parent script.

I am now calling the child script...

###############################################
I am the child script.
What would you like my exit status to be: 3
Exiting with exit status: 3
###############################################

We have now obtained the exit status of the child (exit status: 3).
We can determine what to do (exit with bad exit code or live on).

Bad exit from child... exiting.


GOOD EXIT STATUS (0)
Code:
[user@host ~]$ ./test-parent.sh 
I am the parent script.

I am now calling the child script...

###############################################
I am the child script.
What would you like my exit status to be: 0
Exiting with exit status: 0
###############################################

We have now obtained the exit status of the child (exit status: 0).
We can determine what to do (exit with bad exit code or live on).

The child exited gracefully with exit status '0'
We can now proceed doing other tasks as usual.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Weeeeeee other tasks.

This User Gave Thanks to ddreggors For This Post:
# 4  
Old 09-28-2011
sorry of the dummy question:
if [ "${XSTAT}" -gt 0 ]; then

is -gt means not equal to 0?
# 5  
Old 09-28-2011
Quote:
Originally Posted by khchong
sorry of the dummy question:
if [ "${XSTAT}" -gt 0 ]; then

is -gt means not equal to 0?
Have a read the Man Page for test.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script

I am using mutt on ksh Unix to send emails to addresses plucked from the database. If the "To:" email address is not longer valid and so the email is not sent to the "To:" recipient, but is sent to the valid cc address, I need to be able to get an error code returned to the shell script so that... (3 Replies)
Discussion started by: jzuber
3 Replies

2. Shell Programming and Scripting

How could I use the value of return code

Hello, I am woring on a script where I am getting strange situation.This script actually fetch the source code and tar that code and send to NAS location.This code resides in MKS tool...and we are fetching the source code on checkpoint label basis and script is working fine.First it synch the... (0 Replies)
Discussion started by: anuragpgtgerman
0 Replies

3. Shell Programming and Scripting

Writing a UNIX script from LOG to provide return code.

Folks - Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX... (4 Replies)
Discussion started by: sk72
4 Replies

4. HP-UX

return code from oracle to unix script

Hi I'm writing a shell script that connects to oracle database and fires query to check the availability of data in a table. In case of no data found then what will be the return code and how to handle in that in variable. Kindly provide with an example for better understanding... Thanks... (1 Reply)
Discussion started by: ksailesh
1 Replies

5. Shell Programming and Scripting

return code help

Hello folks, I have a question that if i type ls command and type echo $? it always show "0", how i could do this change that when i type ls it will show me 1, actually i want to change the return code of commands from 0 to 1. Thanks Bash (5 Replies)
Discussion started by: learnbash
5 Replies

6. Shell Programming and Scripting

Need help with return code 1...

Hi Guys,, I am having a unix script which is running the DB2 Insert command. For the insert command, there were no records to be updated. SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. SQLSTATE=02000 + + echo 1 STAGE_RC=1 + ] ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

8. Shell Programming and Scripting

asking about return code

hi all my system is linux red hat i have a script that runs some object . the object return some code to the system i see the code by writing echo $? i want to ask in the script if $? equals 14 how shell is do that in the script thanks (3 Replies)
Discussion started by: naamas03
3 Replies

9. Shell Programming and Scripting

return code of a unix command

How to find out whether the command I executed is successful or unsuccessful(at commandlinet) Eg: say i execute the following command at command line rm * How do i find out whether my previous command is a success or failure. Thankyou. Best Regards, Ram. (1 Reply)
Discussion started by: ramky79
1 Replies

10. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question