Quitting a bash script... any alternatives to exit?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quitting a bash script... any alternatives to exit?
# 1  
Old 01-14-2009
Quitting a bash script... any alternatives to exit?

Folks,
Below is a basic synopsis of the problem. I have a script that I need to check for some env vars and fail (exit the script) if they are not there. At the same time I need to set some default env vars. To do this I must run the script from the parent shell or source the script. Doing this means my call to exit kills my shell. Any ideas?

--
#!/bin/sh

Defined()
{
[ "${!1-one}" == "${!1-two}" ]
}

Fail()
{
echo $(date +%H:%M:%S) ERROR: $1
exit 1
}

if ! Defined FOO; then
Fail "FOO is not defined"
fi

export BAR=/foo/bar

# end script

~$ . ./test.sh # Need to run it this way for it to export my env vars

This exits my running shell


Is there any way to workaround this, such as goto in perl? Or other way to export my env vars without having to run in the parent shell? Thanks in advance.
# 2  
Old 01-14-2009
If you source the script, use return instead of exit.
# 3  
Old 01-15-2009
You can also use structure to avoid calling either exit or return.
Code:
...
if ! Defined FOO; then
    echo "$(date +%H:%M:%S) ERROR:  FOO is not defined"
else
    export BAR=/foo/bar
fi

# 4  
Old 10-04-2010
A word from member : https://www.unix.com/members/302085081.html

1.
Regarding "Quitting a bash script": The thread is closed so I couldn't post there, but I did want to share this.


Code:
      exit


exits the shell, which is usually not what you want if the script was
Code:

source


d.

An alternative is to use
Code:
      kill -SIGINT $$


which sends an interrupt to the shell process (whose pid is stored in the $$ variable).

You can also use this test to determine if a bash script is being run sourced or was run as its own command:


Code:
x"${BASH_SOURCE[0]}" == x"$0"

If this is true, then it isn't sourced, if it's false (the scriptname is not $0) it was sourced.

Last edited by vbe; 10-04-2010 at 05:53 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

2. Shell Programming and Scripting

Bash to exit on no reponse

The below bash does not seem to exit if the user response is n N no No. Is the synatax incorrect? Thank you :). Bash read -r -p "Is this correct? " response if ] then echo "The files do not match" && exit ;; else .... do something fi (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

4. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

5. Ubuntu

Exit user in bash script

I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen: sudo xhost +SI:localuser:lightdm sudo su lightdm -s /bin/bash gsettings set com.canonical.unity-greeter draw-grid false The problem is that... (3 Replies)
Discussion started by: maerlyngb
3 Replies

6. Shell Programming and Scripting

Global exit listener in bash

I have a script with a whole lot of different functions and want to set teh script so that at any point a key or series of keys can be pressed to exit out and into the main menu function. Rather this than endlessly creating, 'Return to main' menus. Something along the lines of Ctrl+q for example... (1 Reply)
Discussion started by: 3therk1ll
1 Replies

7. Shell Programming and Scripting

Bash Shell Script Exit Codes

Here is my daily stupid question: How can I tell a script to only execute if the other scripts exits successfully? So "script A" executes and it executes successfully (0),then "script B" will run or else "script A "executes and it exits unsucessfully (1) then "script B" will read return... (6 Replies)
Discussion started by: metallica1973
6 Replies

8. Shell Programming and Scripting

Bash script wont exit?

Solved Stupidly I didn't put brackets around the , thanks for all the help guys if ps ax | grep Cluster__check.bash | grep -v grep > /dev/null -- fails (if ps ax | grep Cluster__check.bash | grep -v grep > /dev/null) --works (3 Replies)
Discussion started by: danmc
3 Replies

9. Shell Programming and Scripting

Functions, exit, and kill in bash

Hello Okay, for reasons related to sourcing a script from another script, I've had to put my main loop into a function, and from there I call other functions. My problem then is exiting from deep within the function call stack. I used to simply call exit, and that would accomplish what I... (1 Reply)
Discussion started by: brsett
1 Replies

10. Shell Programming and Scripting

Quitting from a script, either sourced or not

This is a very simple problem, I am wondering why I can find no answer anywhere... I have a script that can be run either sourced or not. This script has some place where it needs to quit execution (e.g., when an error is found) If I "exit", the sourced call would exit the parent shell, but... (7 Replies)
Discussion started by: MadMage
7 Replies
Login or Register to Ask a Question