|
QShell for iSeries Authors: Holt T., Kulack F. A. Published year: 2003 Pages: 101-103/219 |
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 replaces the current copy of the Qshell interpreter with the command. When that program ends, control does not return to the Qshell interpreter. Instead, the process ends.
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 must be replaced by the value of the first positions parameter. This process, known as word expansion , consists of four steps:
Tilde expansion, parameter expansion, command substitution, and arithmetic expansion
Field splitting
Path- name expansion (globbing)
Quote removal
Tilde expansion, parameter expansion, command substitution, and arithmetic expansion all have the same precedence. They are done as follows :
Tilde expansion Qshell replaces ~ and ~ user with the name of the users home directory. Qshell replaces ~+ with the value of the PWD variable.
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 surrounded by $(and) or backquotes and replaces the command with its output.
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 necessarily be a one-to-one correspondence between the number of delimited tokens in the original command and the number of fields resulting from field expansion, as an original token may be divided into more than one field.
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 characters .
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 passes through the command before execution.
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 perfectly good variable assignments. To get the system to execute those variable assignments, use command substitution and eval . Command substitution replaces the date command with its output, three assignment commands. The eval utility then passes the three assignments to Qshell for execution. The result is that the current year, month, and day are written to variables sysYear , sysMonth , and sysDay , respectively.
|
|
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 stdout before executing it.
|
|
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 $
|
|
|
QShell for iSeries Authors: Holt T., Kulack F. A. Published year: 2003 Pages: 101-103/219 |
![]() SQL for eServer i5 and iSeries | ![]() SQL Built-In Functions and Stored Procedures: The i5/iSeries Programmer's Guide | ![]() IBM System i APIs at Work | ![]() Advanced Integrated RPG |