"Odd" behavior exiting shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "Odd" behavior exiting shell script
# 1  
Old 03-12-2010
"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option?

For example, if I have the example script (we'll name it ex.sh):

Code:
#!/bin/sh
if [ $# -ne 1 ]
then
  echo "Bye."
  exit 2
fi

And I execute it like this:

Code:
>./ex.sh

It says "Bye." and then returns me to my terminal. If however, I execute it like this:

Code:
>. ./ex.sh

It acts the same as above, but my terminal session is terminated after it exits. Is this normal? If so, can I avoid it? It does not seem to matter what the exit status is.... The real script will be setting environment variables that the calling environment will need, so I need the affect of ". ./ex.sh" without the ill effect of terminating the calling shell if it fails for some reason (I want the caller to see the failure and act accordingly).

For what it's worth, the caller will be (.profile) which is why this is very undesirable, becasue if it fails then the user will never see why and never be able to log in (unless they su without the profile executing). Not good....

O/S is AIX 5.3 btw.
# 2  
Old 03-12-2010
You are trying to execute the script in the same shell.

Use sh ./ex.sh to run it in a new shell, where the exit will get back to your original shell.
# 3  
Old 03-12-2010
Quote:
Originally Posted by dennis.jacob
You are trying to execute the script in the same shell.

Use sh ./ex.sh to run it in a new shell, where the exit will get back to your original shell.
That won't work because then the environment variables I need to export won't get back to the original shell.
# 4  
Old 03-12-2010
Code:
#!/bin/sh
if [ $# -ne 1 ]
then
  echo "Bye."
  exit 2
fi


The variable $# is the number of parameters provided to the script when calling the script.
When called from the command line as "ex.sh" with no parameters $# has the value zero which is not equal to one. The script then outputs "Bye" and exits. This does not log you out because the script was executed in a self-contained shell.

When called with ". ./ex.sh" the script is executed in the current shell. Again because the number of parameters supplied was zero the shell outputs "Bye" and then issues an "exit" to the current shell which logs you out.

Try providing exactly one parameter and see if the result is different:
Code:
./ex.sh myparameter
. ./ex.sh myparameter


Last edited by pludi; 03-16-2010 at 03:04 AM..
# 5  
Old 03-15-2010
Quote:
Originally Posted by methyl
The variable $# is the number of parameters provided to the script when calling the script.
When called from the command line as "ex.sh" with no parameters $# has the value zero which is not equal to one. The script then outputs "Bye" and exits. This does not log you out because the script was executed in a self-contained shell.

When called with ". ./ex.sh" the script is executed in the current shell. Again because the number of parameters supplied was zero the shell outputs "Bye" and then issues an "exit" to the current shell which logs you out.

Try providing exactly one parameter and see if the result is different:
Code:
./ex.sh myparameter
. ./ex.sh myparameter

I know all that, lol. I think maybe I should ask this succinctly as follows:

If I want to prematurely terminate a shell script, executed with the ". ./script.sh" syntax, how can I do it without terminating the calling shell?

Since there is no goto in ksh that I can use to get to the end of the script, I don't see how to stop it at "Bye" with out using exit or wrapping the whole script in the if/else block. The actual script I'm doing this with is much more complicated, this trivial script is just to show the behavior I was experiencing.

I suppose the answer is "this is normal, the script is executing under the context of the current shell". So, the next question is, how else to stop the script without exitting the shell?
# 6  
Old 03-15-2010
if you're going to source the script in this way, use "return" instead of "exit". it'll return control back to the calling session.
# 7  
Old 03-17-2010
Quote:
Originally Posted by curleb
if you're going to source the script in this way, use "return" instead of "exit". it'll return control back to the calling session.
that works! thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Solaris

Solaris 10 "Exiting (caught signal 11)"

I get an error after the initializing screen. I am using a DVD/ROM to boot up the installation on a Dell Inspiron 1520. Segmentation fault - core dumped. I have tried to restart multiple times. Please help (1 Reply)
Discussion started by: Jimasaurus
1 Replies

3. Shell Programming and Scripting

Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document <<EOF echo "Sum is: `expr $1 + $2`" EOF Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Ubuntu

What is solution for this error "tar: Exiting with failure status due to previous errors"?

Does anyone know what is solution for this error ?tar: Exiting with failure status due to previous errors from last 3 days I am trying to take backup of home/user directory getting again and again same error please anyone give me solution (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

5. UNIX for Advanced & Expert Users

"╭─ " Character combo in $PATH causes strange autocompletion behavior in zsh

I've posted about this before, but only recently narrowed the problem down to a specific cause. Ok, first of all, the behavior: It occurs when autocompletion brings up its list (not when there is only a single option). Basically, if I were to type, say, cd ~/<TAB> I would get something... (2 Replies)
Discussion started by: marshaul
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies

8. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question