LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to exit out of the calling Shell Script (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-exit-out-of-the-calling-shell-script-748535/)

harimac 08-18-2009 04:38 PM

how to exit out of the calling Shell Script
 
Hi All,

I have 2 shell scripts, script1.sh and script2.sh. I call script2.sh from within script1.sh ( by simple ./script2.sh command).

Based on some condition, i use exit 0 to exit out of script2.sh. I was trying to find if i can exit out of script1.sh as well at once.

Can anyone please clarify, if there is a way to do that.

below is the example

script1.sh
#!/bin/bash

echo "Before ..."
./script2.sh
echo "After ..."


script2.sh
#!/bin/bash

# check if a file exists and is not empty, if not empty exit (but i want to exit out of script1 as well )

if [[ -s err.log ]]; then
echo " There was problem file is empty "
cat err.log
echo " The Script will Exit. Please fix the issue and run again..."
exit 0
else
echo " file is Empty. Proceeding with the next step... "
fi



----
When i execute below is the output

Before ...
There was problem file is empty
The Script will Exit. Please fix the issue and run again...
After ...

I am trying to exit out of script1.sh as well so that i dont print "After ..."

thanks for the help

catkin 08-18-2009 04:45 PM

script1.sh
Code:

#!/bin/bash

echo "Before ..."
./script2.sh || exit
echo "After ..."


colucix 08-18-2009 05:08 PM

A little add-on to the catkin's solution: script2.sh should return a status code different from 0, otherwise script1.sh will receive a "success" status:
Code:

exit 1
Another - less graceful - way is to kill the parent process from script2.sh:
Code:

kill -9 $PPID

catkin 08-19-2009 01:49 AM

Quote:

Originally Posted by colucix (Post 3648598)
A little add-on to the catkin's solution

Thank you colucix, I missed that and assumed script2.sh was setting non-zero exit status on error. The dangers of ASSumptions!


All times are GMT -5. The time now is 02:26 PM.