LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-18-2009, 04:38 PM   #1
harimac
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
Question 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
 
Old 08-18-2009, 04:45 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
script1.sh
Code:
#!/bin/bash

echo "Before ..."
./script2.sh || exit
echo "After ..."
 
Old 08-18-2009, 05:08 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 08-19-2009, 01:49 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by colucix View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to properly exit functions in Shell Script? Jude Terror Programming 5 01-27-2009 03:15 PM
Calling PHP5 from a shell script Dafydd Programming 2 10-31-2008 07:31 PM
calling function shell script very simple ones knockout_artist Linux - Newbie 2 05-15-2008 12:01 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration