Module 44 exec

Previous Table of Contents Next


Module 44
exec

DESCRIPTION

The internal exec command replaces the current shell process with the specified command. Normally, when you run a command a new process is spawned. The exec command does not spawn a new process. Instead, the current process is overlaid with the new command.

It may also be used to set redirection file descriptors. In this situation the current shell process is modified but not overlaid.

COMMAND FORMAT

Following is the general format of the exec command.

 exec [ command ] [ arg ... ]      exec fd< file      exec fd> file 

Arguments

The following list describes the arguments that may be passed to the exec command.

command An executable command. The command replaces the current shell in memory without spawning a new process. If you cannot execute the command for some reason, then you exit the current shell.
arg Arguments to command.
fd< file Opens file for input with file descriptor fd.
fd> file Opens file for output with file descriptor fd.

C Shell
The C shell does not support using exec to change file descriptors (fd).

FURTHER DISCUSSION

If you specify a command, the shell replaces itself in memory with the new command you specified. For example,

 exec vi myfile 

replaces the shell with the vi program text and places you in vi, editing myfile. When you exit vi you exit the system because vi has become your login interface to the operating system.

Redirection

You can use exec to perform I/O redirection commands. The following examples illustrate the use of exec for redirection purposes.

exec 3< inputfile # Opens inputfile with file descriptor 3 for reading.
exec 4> outputfile # Opens outputfile with file descriptor 4 for writing.
exec 5<&0 # Makes fd 5 a copy of fd 0 (standard input).
exec 6>&p # Attach fd 6 to co-process.

RELATED FILES

The exec command can be used to open , close, and copy file descriptors. If you do not specify a command or arguments, you can specify redirection symbols and file descriptors to perform these functions.

APPLICATIONS

You can use the exec command to replace the current shell without creating a new process. This becomes useful in a set of shell scripts that execute one another. Instead of spawning new processes each time you call a different shell, you can exec to the new shell.

The exec command may also be used to open, close, and copy file descriptors as specified by I/O redirection.

TYPICAL OPERATION

In this activity you create a script which execs mailx or an editor. Thus when you exit mailx or the editor you return to your shell not this script.

1.    Type the following lines using the editor of your choice.
 while true      do      echo "Go to Mail, Editor, or eXit:"      read ANS      case "$ANS" in           [mM]) exec MAILX ;;           [eE]) exec ${EDITOR:=vi} ;;           [xX]) ezec exit ;;      esac      done 
2.    Change the modes of the script by typing chmod 755 ash and pressing Return .
3.    Now run the script. Type ash and press Return .
4.    Press Return . Notice the prompt reappears. Now type e to enter the editor. Now exit your editor. Notice the prompt did not reappear. You exited back to the main shell not the script because the script was overlayed by the editor.

C Shell
The csh exec command works the same way. You could write the same script just by changing the syntax for csh.

5.     Turn to Module 48 to continue the learning sequence.


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