[comp.unix.shell] Exit in Bourne Shell

edward@jts.com (Edward Tsia) (09/15/90)

I am currently having problems understanding why a command inside a while-do-
done loop calling a function which calls exit does not exit the shell, but only
breaks from the while loop.

I have found this to be the case only for a while loop which accepts input from
a redirected file; if standard input (keyboard) is used, exit works as
anticipated.  The following is an example:

<beginning of script>
stop () {
	exit 0
}

while read a
do
	stop
	echo $a
done < junk.data
echo hello2
<end of script>

The accompanying junk.data file could be:

<beginning of junk.data>
hello
hello
...
hello
<end of junk.data>

As stated above, this script, upon execution, would echo 'hello2', although it
obviously should call function stop which in turn should terminate the shell.
However, if the redirection from junk.data is removed and the input is
received from keyboard, the exit call does terminate the shell.

I wonder whether this behaviour is caused by the fact that a subshell is 
generated when the while-do-done loop is started and the 'exit' only causes 
this child process to terminate, thus returning to its parent to continue
processing.

I would appreciate any comments which may shed any light on this matter.
I'd like to thank you in advance for your help.

Ed.

--