how to exit out of the calling Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to exit out of the calling Shell Script
# 1  
Old 08-18-2009
how to exit out of the calling Shell Script

Hi All,

I tried looking for this, but was not able to get a clear answer. Hence posting here. Please find the details below. Thanks for the help

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
Code:
#!/bin/bash

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


script2.sh
Code:
#!/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

edit by bakunin: added code-tags. As this is your first post i will not charge you the usual rate (many, many bits, which will make me stinking rich) out of charity. Still, i would like to see you use them yourself in the future instead of relying on me to provide them afterwards. Thanks for your consideration.

Last edited by bakunin; 08-18-2009 at 07:50 PM..
# 2  
Old 08-18-2009
Maybe I'm missing something, or have simply had too much caffeine today but why don't you just put another exit inside of script 1?

Code:
./script2.sh
exit 0


Is there more code in that script that needs to be ran or something?
# 3  
Old 08-18-2009
have a none-zero exit code from script2 and get script1 to check for the exit code before proceeding.

Exit Shell Script Based on Process Exit Code - Stack Overflow
# 4  
Old 08-18-2009
Quote:
Originally Posted by chompy
Maybe I'm missing something, or have simply had too much caffeine today but why don't you just put another exit inside of script 1?

Code:
./script2.sh
exit 0


Is there more code in that script that needs to be ran or something?
I think he only wants to exit script1 under certain conditions (eg some occations exit just script2 and other times exit both scripts)
The only way I know to do this is by checking exit codes.

Using the link in my previous post as a template:
Code:
./script2.sh
rc=$?
if [[ $rc != 0 ]] ; then
    exit 0
fi


[edit]

Odd, I can't see my previous post in this thread.
Well here's the link anyway (you might find it useful):
http://stackoverflow.com/questions/9...cess-exit-code

There's probably better examples online - that was just a quick google on exit codes. But it should give you an idea on how to tackle the problem
# 5  
Old 08-18-2009
thanks for the response chompy, but the check i do is in script2.sh, if it fails , i want to exit form both the script, if not i want to continue with the rest of the script.

sorry if i was not clear.

thanks
Hari

---------- Post updated at 05:49 PM ---------- Previous update was at 05:44 PM ----------

laumars thanks for the response, i tried it and it works. with this i need to check after i call a shell script every time ( i can live with that Smilie . Not trying to be lazy , but would have been great if we had a way to exit out of the chain of calling shellScripts at once.

thanks much for all your comments
# 6  
Old 08-18-2009
There might be an easier way (if shell scripts are getting this messy then I usually build the routines in another language, so I'm not the best authorety on the subject) but one "kill all" alternative could be killing the parent process.
However, getting the process ID might prove just as lenghy as checking the exit codes.

Personally, I'd opt for exit codes as you can get the parent script to perform different routines depending on the exit code of it's children (so you're not just stuck with a boolean "die" or "continue")

I hope that makes sense. It's getting late in the UK so my english is falling apart....
# 7  
Old 08-18-2009
laumars is correct. Your scripts have several design flaws and you should definitely use exit codes to pass information from one script to the other.

Firstly, you exit script2 with an exit code of 0 instead of something non-zero. The convention is to return 0 only if everything works correctly and any other value designating one error condition. When i write scripts one of the first things (even before i write the actual code) is to think the "interface" to the outside: this is the parameter(s) the script needs and what could go wrong and writing down different error codes for everything. Like in the following:

script: foo.sh [string filename] [int linenumber] [string word]
0=ok (word found at linenumber)
1=word not found
2=file not found
3=misc. error

As you can see you could even guess right now what the script should do, without having seen any code: it searches for a word in a given line number in a given file and returns 0 if it finds it there, 1 otherwise. You don't need much more documentation than this to know everything you need to know about it if you want to call it from your script, correct?

Second, your scripts terminate simply somewhere. This works, because at the end of every script the shell implies an "exit 0", if the end of file is reached. Relying on this is not good style, get control over your scripts termination. Your script2 could look like:

Code:
#!/bin/bash
if [[ -s err.log ]]; then
     cat err.log
     exit 0
   fi
exit 1

You can see that i removed the messages too. The reason is that you should write every script to be as versatile as possible. Putting error messages into it may be fine within the context of script1, but probably not in the context of some other script you will write and which might want to use script2 too. Therefore put the error messages in script1 and the pure functionality into script2.

Lets see script1 after some changes according to this:

Code:
#!/bin/bash

echo "Before ..."
./script2.sh
if [ $? -gt 0 ] ; then
     echo "err.log has size 0 - please check!"
     echo "exiting... "
     exit 1
fi
echo "After ..."
exit 0

Now we have one last problem: as you can see the name off the file - "err.log" - is used several times throughout both scripts. Suppose you change the name for some reason. You would have to change it on three different places in two different scripts. Not good and error-prone. We change the name of the file to a variable which we well pass to script2 as a parameter, making the script even more versatile, because we could immediately use it to display another file without even changing it - simply by passing another filename as parameter as we call it:

script2:
Code:
#!/bin/bash
# display a file passed in $1 and return 1 if it is size 0, otherwise 0

local file="$1"

if [[ -s "${file}" ]]; then
     cat "${file}"
     exit 0
   fi
exit 1

script1:
Code:
#!/bin/bash

local file="err.log"

echo "Before ..."

./script2.sh "${file}"
if [ $? -gt 0 ] ; then
     echo "${file} has size 0 - please check!"
     echo "exiting... "
     exit 1
fi
echo "After ..."
exit 0

A last remark is the usage of "./script2.sh". Do NEVER do this, because it relies on your current shell sessions PWD pointing to where the scripts are located. Store both these scripts in directory /tmp/foo, change to there and call script1 via "./script1" - it will work. Now change to /tmp and call it by "foo/script1" - it will fail, because it will not find script2.

This is a good way to make things complicated for you, especially if your script get more and more complicated. Therefore always use fully qualified paths. You could - again, to make possible changes in the future easier to accomplish - use variables to name these paths, here is a last version of script1, supposed that alll your scripts are located in /tmp/foo:

script1:
Code:
#!/bin/bash

local scriptpath="/tmp/foo"
local file="err.log"
# maybe: local="${filepath}/err.log" where filepath is defined previously

echo "Before ..."

${scriptpath}/script2.sh "${file}"
if [ $? -gt 0 ] ; then
     echo "${file} has size 0 - please check!"
     echo "exiting... "
     exit 1
fi
echo "After ..."
exit 0

I hope this helps.

bakunin

Last edited by bakunin; 08-18-2009 at 08:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit the shell script

Hi, suppose my script is sample.sh i have to run using '. ./sample.sh' as . ./script file always executes the script in my parent shell. when my sample.sh contains exit command .. my environment is getting closed as am executing in the parent shell ... please suggest me how can i use... (5 Replies)
Discussion started by: pracheth
5 Replies

2. Programming

Gcc error when calling exit(-1) in a .c file

gcc is giving me an error when calling exit(-1) in xbuplot.c cd ../pltlib; make xbuplot.o make: Entering directory `/media/ios120/chrisd/research/fast-zb/fast/pltlib' gcc -c -o xbuplot.o xbuplot.c xbuplot.c: In function ‘xbuinit_': xbuplot.c:171:5: warning: incompatible implicit... (5 Replies)
Discussion started by: kristinu
5 Replies

3. Shell Programming and Scripting

exit shell from a script

hi guys I have a script that I need to terminate or exit the shell or session completely for the user but the exit only exit from the script and takes the user to the shell I found this https://www.unix.com/unix-dummies-questions-answers/399-using-exit-command-shell-script.html saying that... (1 Reply)
Discussion started by: kopper
1 Replies

4. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

5. Shell Programming and Scripting

Calling another shell script

Hi there, I have an script reading content of a file and runs whatever command is specified there, as follows #!/bin/bash # Supposed to read from a file that commands are listed to be run # when the server starts for initialization CMD_FILE=/myScripts/startup/task2do.txt if ; then ... (2 Replies)
Discussion started by: james gordon
2 Replies

6. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

7. Shell Programming and Scripting

Calling Shell Script

Hello Friends, I have bash script on unix server which i want to call from windows server. Basically i want a command line which will call this script on unix server. Any one has any idea regarding this? Help really appreciated!! Thanks, Roshni. (1 Reply)
Discussion started by: onlyroshni
1 Replies

8. Shell Programming and Scripting

exit a shell script!!

could somebody tell me please how to exit a shell script: if then echo "No arguments detected" exit 1 fi ... echo "still there" # is displayed .. :-( (4 Replies)
Discussion started by: sami98
4 Replies

9. Shell Programming and Scripting

Passing exit code to calling script

In production I need to pass an exit code from a script that is being called 3 or 4 layers deep. I've created test scripts to play with it until I get it right. As you can tell, this is not my normal job. Perhaps I should have entered this in UNIX for Dummies section. Anyway, I keep losing the... (2 Replies)
Discussion started by: debbiekuch
2 Replies

10. Shell Programming and Scripting

Calling shell script ?

hi friends, i'm new to unix and straight away i had to start with the script files. I've a script file which gets called from a menu item on a GUI. This script file again calls .awk file, in performing some tasks , which also generates certain files. I modified the files to generate some... (1 Reply)
Discussion started by: Ravi_Kandula
1 Replies
Login or Register to Ask a Question