The exec utility is used primarily for controlling file descriptors, but it has another function: You may use it to replace an executing program with another program. No new process is created. This is similar to CLs Transfer Control command (TFRCTL) or the Unix-type C API that is also named exec().
When you run a command via
exec
, Qshell
This matches the series of steps that Qshell goes through when creating a new process. The new process begins by running the shell, then uses exec to run the command passed to it, since there is no need to return to the shell.
Before Qshell can execute a command, it must make substitutions
for certain tokens in the command. For example, all occurrences of
the string
$1
must be
Tilde expansion, parameter expansion, command substitution, and arithmetic expansion
Field splitting
Path-
Quote removal
Tilde expansion, parameter expansion, command substitution, and
arithmetic expansion all have the same precedence. They are done as
Tilde expansion
Qshell
Parameter expansion Qshell replaces $ n and ${ n }with the value of the n th positional parameter. Qshell replaces $ var and ${ var } with the value of a variable. (Parameter expansion is discussed in chapter 6.)
Command substitution
As mentioned
earlier in this chapter, Qshell executes a command that is
Arithmetic expansion Qshell replaces arithmetic expressions surrounded by $((and)) with their calculated values. (Arithmetic expansion is also discussed in chapter 6.)
If the IFS variable is not null, Qshell uses the value of the
IFS variable to divide the command into tokens. This process is
called
field splitting
. There will not
Path-name expansion is more commonly known as globbing . This step is omitted if the noglob option has been set. (Globbing is discussed in more detail in chapter 14.)
The final step is to remove the single quote, double quote, and
backslash quotation
At this point, the command is passed to the Qshell interpreter for execution.
Qshells normal method of operation is to retrieve a command,
make appropriate substitutions, and execute the result. The
eval
utility causes Qshell to make two
The eval utility constructs a command string, which it then passes to Qshell for execution. It is similar in concept to the QCMDEXC or the C system() API, which executes a CL command stored in a string.
For example, consider Figure 12.30. To understand how this
example works, execute the
date
command by itself. If the
date is January 2, 2003,
date
sends the string
sysYear=03
sysMonth=01 sysDay=02
to standard output. Notice that the
string contains three
|
|
date +sysYear=%y sysMonth=%m sysDay=%d sysYear=03 sysMonth=09 sysDay=09 /home/jsmith $ eval $(date +sysYear=%y sysMonth=%m sysDay=%d) /home/jsmith $ print $sysMonth 01 /home/jsmith $ print $sysDay 02 /home/jsmith $ print $sysYear 03
|
|
To view what steps
eval
is taking, set the
xtrace
option, as shown in Figure 12.31. This option causes Qshell to
write each command to
|
|
set -x /home/jsmith $ eval $(date +sysYear=%y sysMonth=%m sysDay=%d) + date +sysYear=%y sysMonth=%m sysDay=%d + date +sysYear=%y sysMonth=%m sysDay=%d + eval sysYear=03 sysMonth=01 sysDay=02 + sysYear=03 sysMonth=01 sysDay=02 /home/jsmith $
|
|