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
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):
#!/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):
#!/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)
[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)
[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.