Quitting from a script, either sourced or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quitting from a script, either sourced or not
# 1  
Old 05-18-2010
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 if I "return", the non-sourced call is not allowed.

How to solve this?
# 2  
Old 05-18-2010
I am not sure what do you mean by sourced?
# 3  
Old 05-18-2010
Using return should be analogous to exit in both cases...without the pesky side-effect of killing the parent.

What exactly is the issue? Have you traced the process with set -x? [embed set -x into each function in use to capture the flow across each subprocess...]
# 4  
Old 05-18-2010
I cannot "return" from a non-sourced script

$ ./test
./test: line 2: return: can only `return' from a function or sourced script

---------- Post updated at 06:09 PM ---------- Previous update was at 06:08 PM ----------

Quote:
Originally Posted by panyam
I am not sure what do you mean by sourced?
sourced:

Code:
$ . ./test

not sourced:

Code:
$ ./test

# 5  
Old 05-18-2010
Any script could run sourced, or not.

Why would you need to source the script?

If you do source it, there are implications which could only be overcome programmatically.

The simple solution is: Don't source the script. If you have to source it, then source it from another script, not from the login prompt (if your concern is that you get logged out when you "exit" the script). If you really have to source it from the shell, start a second shell before you source it.
# 6  
Old 05-18-2010
Quote:
Originally Posted by scottn
Any script could run sourced, or not.

Why would you need to source the script?

If you do source it, there are implications which could only be overcome programmatically.

The simple solution is: Don't source the script. If you have to source it, then source it from another script, not from the login prompt (if your concern is that you get logged out when you "exit" the script). If you really have to source it from the shell, start a second shell before you source it.
The main purpose of the script is to set some environment variables, so I MUST source it. However, the same script also manage a set of choices for these variables (i.e., it can create or remove an "environment", list all the "environments", alias an "environment" and then set all the environment variables to the values specified by the particular "environment" chosen). In the "management" case, you do not need to source it, e.g.:

Code:
$ test add an_environment
$ test remove another_environment
$ . test set an_environment

# 7  
Old 05-18-2010
can you post the sourced script?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preloading open() for files sourced by tcsh

I am overriding the file open system calls (open, open64, fopen, fopen64, freopen, freopen64) in a dynamic library and setting LD_PRELOAD to point to that library. Here is a sample script I'm testing this on - #!/bin/tcsh source testcsh1.csh source testbash1.sh On RHEL 5.7 (tcsh 6.14.00), the... (2 Replies)
Discussion started by: endgame
2 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

Problem in quitting/exiting from sqlplus

Hello, This is my first post and I would be very thankful if you can help me. I've already searched in the forum and I've found a very similar thread in wich my problem is solved, but the thread is closed and the solution given in it doesn't work in my shell: 153194-problem-quitting-sqlplus ... (11 Replies)
Discussion started by: JuanPerez
11 Replies

4. Shell Programming and Scripting

Error Installing Env::Sourced

Hello, I am trying to install Env::Sourced and i get the following error. sh: source: not found sh: include.sh: not found Unable to determine your shells source program, typically 'source' or '.' How can i overcome this? Best Regards, Christos (3 Replies)
Discussion started by: chriss_58
3 Replies

5. Shell Programming and Scripting

problem in quitting from sqlplus

Hi all, I have a scenario where im connecting to sqlplus executing a query,redirecting the output to a file and manipulating the file MY CODE IS AS FOLLOWS sqlplus -s /nolog << EOF > $UTILITIES_HOME/temp/analyze.temp.log & set linesize 3000 set trimspool on set pagesize... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

6. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: bashN00b
3 Replies

7. Shell Programming and Scripting

KSH - Sourced file location

The sample scripts below, I am looking for the location of the sourced b.sh file. The b.sh file is source by multiple files, so it is not feasible to rely on a "global" variable or passed argument. Is there any way to determine the location of b.sh within b.sh? a.sh #!/bin/ksh echo "a:... (11 Replies)
Discussion started by: Al262
11 Replies

8. Shell Programming and Scripting

Exit from sourced script

Hello, I have written a script (say chld.sh). its structure is as follows: ------------------------ #!/bin/sh usage() { echo "chld.sh <param1> <param2>" exit 0 } chk_param_1() { case $param1 in c) export PATH=$PATH:/home_a/bin/execs;; d) export... (2 Replies)
Discussion started by: angad.makkar
2 Replies

9. OS X (Apple)

which file is sourced by bash on login (Mac OS X 10.5.3)?

Hi: So my current Python (2.52) rig is not working as intended. "echo $PATH" in bash gives me 'X'" that's not what i want, so i need to change my path. To do that, there appeared to be four choices (all in my ~/ directory--note: I'm root, it's my Mac, but i'm in a user account): .profile... (2 Replies)
Discussion started by: Alex_Land
2 Replies

10. Solaris

Emacs changes the term colors after quitting

Ok. So I've finally compiled the latest version of GNU emacs on Solaris 5.8. I've set putty up so that it sets TERM=xtermc. emacs now has syntax highlighting. The problem is, whenever I quit emacs, it changes the screen colors. The background is now the same as the color of the minibuffer. It... (1 Reply)
Discussion started by: m9dhatter
1 Replies
Login or Register to Ask a Question