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):
And I execute it like this:
It says "Bye." and then returns me to my terminal. If however, I execute it like this:
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....
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:
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:
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?
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)
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)
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)
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)
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)
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)
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)
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)
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)