UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 3.5 Stop Accidental C Shell Logouts Chapter 3
Logging Out
Next: 3.7 Detaching a Session with screen
 

3.6 Stop Accidental Bourne Shell Logouts

It's pretty easy to type one too many CTRL-d characters and log out of a Bourne shell without meaning to. The C shell has an ignoreeof shell variable (3.5) that won't let you log out with CTRL-d. So do the Korn shell and bash; use set -o ignoreeof.

Here's a different sort of solution for the Bourne shell. When you end the shell, it asks if you're sure. If you don't answer yes, a new shell is started to replace your old one.

First, make a file like the C shell's .logout that will be read whenyour Bourne shell exits . (3.2) Save your tty (3.8) name in an environment variable (6.1), too-you'll need it later:

trap 
TTY=`tty`; export TTY
trap '. $HOME/.sh_logout; exit' 0

(Your system may need $LOGDIR instead of $HOME.) Put the following lines in your new .sh_logout file:

exec < 


case 

exec -sh 
exec < $TTY
echo "Do you really want to log out? \c"
read ans
case "$ans" in
[Yy]*) ;;
*)  exec $HOME/bin/-sh ;;
esac

The last line is some trickery to start a new login shell (51.9). The shell closes your tty (45.20) before reading your .sh_logout file; the exec < $TTY reconnects the shell's standard input to your terminal.

Note that if your system is very slow, you may not get the reminder message for a couple of seconds-you might forget that it's coming and walk away. That hasn't been a problem where I've tested this. If it is for you though, replace the read ans with a program like grabchars (45.32) that times out and gives a default answer after a while. There may be some Bourne shells that need other tricks-and others that don't need these tricks-but this should give you an idea of what to do.

- JP


Previous: 3.5 Stop Accidental C Shell Logouts UNIX Power ToolsNext: 3.7 Detaching a Session with screen
3.5 Stop Accidental C Shell Logouts Book Index3.7 Detaching a Session with screen

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System