Module 45 exit (csh, ksh, sh) logout (csh)

Previous Table of Contents Next


Module 45
exit (csh, ksh, sh) / logout (csh)

DESCRIPTION

The internal exit command causes the current shell to terminate. If the current shell is a child process, execution returns to the parent process when it exits. If the current shell is the login shell, you re logged off the system when the shell terminates.

The Ctrl-D character can be used to perform the same function. The Ctrl-D informs the shell it has reached the end-of-file (EOF). Because the shell is a filter and filters terminate when EOF is reached, the shell terminates and you log off the system or return to the parent process.

If you are writing a shell script and you need to provide a return code (exit status) to a parent process, you can use the exit n format, where n is the return code you want returned to the calling (parent) program.

COMMAND FORMAT

Following is the general format of the exit and logout commands.

 exit [ n ]      Ctrl-D 

C Shell
exit [n]
logout
Ctrl-D

Options

Only one option may be used to control how exit functions.

n The return code (exit status) you wish to return from the termination of the current shell

FURTHER DISCUSSION

To enter a subshell you have to run the ksh , csh , or sh program. For example, you would type

 cj> ksh 

and press Return. To return to your parent shell you type

 cj> exit 

and press Return. The exit and Ctrl-D are handled a little differently by the shell. An exit command is processed as a normal internal command. Whereas the Ctrl-D is interpreted as EOF (end-of-file) and causes the shell to exit immediately. By using exit you can specify a return code value. This value is stored in the parent shell variable $? ($status-csh). An example of how you might use this return code and variable will help explain return codes.

Suppose you want to write a script named readable that returns a 0 if a file is readable and a 1 if the file is not readable.

 if [ -r "" ]      then exit 0      else exit 1      fi 

You pass readable a single argument, which is a filename. If the file is a readable file, the return code is a 0; otherwise , a 1 is returned. Now in your parent shell you can use the $? ($status-csh) variable to test whether the file is readable are not. For example, your parent shell might look like this:

 readable somefile      if [ $? ]      then echo "Processing the file"      ...      else echo "Cannot read the file"      fi 

This provides you with the capability to tell whether a child shell script completed with or without errors.


C Shell
The logout command is an internal csh command that logs you off the system. It only works while in the login shell. If you are in a subshell, the logout command will not perform an exit or logout.
When you type logout and press Return in your login shell, the shell executes the commands in the ~/.logout file before exiting.

The logout command can be duplicated in the ksh by setting an alias that performs an exit for you. Some systems implement logout in the shell, whether it be sh or ksh . It is not implemented on all UNIX systems; therefore, you may want to create it as one of your own aliases. You can make the logout alias perform other functions before logging you out of a shell or off of the system. Refer to Module 4 for more details on alias . The following example is a possible logout alias:

 cj> alias logout="clear; du -s $HOME; times; exit"      cj> logout 

In this example your screen is cleared, your disk space usage is displayed, and the shell status information is displayed before you are logged off the system.

Another alternative is to set a trap in your ~/.profile to execute the ~/.logout file. Simply place the following line in your ~/.profile.

 trap . $HOME/.logout; exit 99 0 

When you log out of the system with the exit command, the shell will trap signal 0 and execute the $HOME/.logout file and then exit the shell.

Logging out of the system is important. By logging out you prevent unfriendly people from causing possible mischief, using your terminal and user ID. If you are away from your terminal for more than a few (5) minutes, log off! If your terminal resides in a secure area, this may not be that important, but it s better to form the habit early than be sorry later.

Logging out provides someone else with a free port (/dev/ttyXX) to use if you are dialing in on a modem. If you are hardwired, this may not make any difference. Some UNIX systems are configured to log you out if you turn your terminal off. Terminals connected via modems usually perform in this manner. Check with your system administrator before you turn your terminal off to log out when using a modem. If the system does not recognize that you dropped the carrier on the modem connect, you will not be logged out. Therefore, the next person that dials in on that system modem will receive your shell prompt instead of the system s login prompt.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net