Using exit in a Bourne shell script

Leo de Wit leo at philmds.UUCP
Thu Jul 21 17:14:45 AEST 1988


In article <16540 at brl-adm.ARPA> iunix1 at almsa-1.arpa (Will Martin) writes:
>Back in February, there were some Info-UNIX discussions under the above
>subject regarding methods of replacing the normal CTRL-D logoff with 
>a special script or procedure. One of the contributors mentioned this:
>
>> From: Kathy Vincent <kathy at bakerst.uucp>
>> 
>> 	trap '$HOME/.logout' 0
>> 
>> Which is to say, "when you receive the logoff signal, execute $HOME/.logout
>> first and THEN log off."
>
>On our Sys V Unisys/Sperry I can do this, and get the ".logout" script to
>execute when I hit a CTRL-D at the shell. It runs whatever I put in
>".logout" and then logs me off the system. However, what I really want to
>happen is to have a script execute that will then ask me "Do you REALLY
>want to log off?" and then either return me to my shell, or allow me to
>log off, depending on my answer. I want to do this because I am

  [some stuff deleted]

>Anybody have any advice on this subject? Is there a clean way to do what
>I want? Or even a dirty way -- I'm pretty sloppy... :-) Can a script
>executed at the "trap 0" point get back to the shell where the signal
>originated, or is it too late by then? If so, could it cause another
>top-level shell to be spawned, keeping all the environment I had before?

I tried the 'trap ....' command and noticed that in the .logout script
a read from the terminal was not honoured, i.e. read answer didn't put
anything in answer (there was no waiting for my response). Obviously
the stdin of the shell has already been closed. Then I tried to fake it
by reading from /dev/tty; you have to do something like

for i in 1
do
    read answer
done </dev/tty

because read cannot have redirection. This time there is a wait, but
again answer is empty (probably because this time read gets executed in
a subshell).  There is also the problem to get a correct standard input
filedescriptor for a new shell I think if the current one is closed
(not sure about that).

However, the following worked (a bit 8-):

1) Put in your .profile:

exec 3<&0                   # Dup stdin
trap '. $HOME/.logout' 0    # note the .; better to let you shell execute it

2) Put in .logout:

------------------     .logout starts here    ------------------

echo -n 'Do you really want to logout [Y/n] ? '

exec 0<&3
read answer

case $answer in
n|N) exec $SHELL;;
*) exec 0<&-;;
esac
------------------     .logout ends here    ------------------

Of course the SHELL environment variable has to contain the name of
your favourite shell. The stdin descriptor is dupped back from 3 (maybe
it is better to use a symbolic name than 3).
The problem I haven't solved yet is to re-install the trap after the
exec (a solution that works for /bin/sh is to exec a small program
instead that execs /bin/sh with argv[0] == "-sh"; the - guarantees that
the .profile is read).
In any case however, all environment variables are inherited from the
current shell. I hope it is sloppy enough for you 8-)!

Have fun!

           Leo.



More information about the Comp.unix.questions mailing list