Flylib.com

Books Software

 
 
 

Qshell for iSeries - page 104


Summary

Qshell executes commands of several types: regular and special built-in utilities, reserved words, aliases, functions, and external files. These commands may be used alone or combined into compound commands.

Each command runs in a process, which is similar to an iSeries job. Some commands run in the current process, while others run in child processes under the shell that spawned them. Child processes do not automatically receive the values of the variables of their calling processes. The export utility causes shell to define variables in child processes.

To find a command, Qshell searches the directories listed in the PATH environment variable. The current directory is not searched unless it is listed in the path .

There are several ways to run a command: in background mode, through command substitution, in the current process under the source utility, through the xargs utility, and through the exec utility.

Before executing a command, Qshell substitutes values for certain tokens through the process of word expansion. The eval utility causes Qshell to make two substitution passes before executing the command.



Chapter 13: Functions

Overview

A Qshell function, like a Qshell script, is a group of commands that is given a name . Functions differ from scripts in that functions become part of the invoking environment, whereas scripts are stored in files. Qshell can execute a function from memory, whereas it must load a script from an external stream file.

Qshell functions are similar to subroutines, functions, and procedures in other languages, and therefore can be used to implement modularity in Qshell scripts. While functions are usually defined in scripts or profile files, at times it is useful to type a function into an interactive session to help automate ad hoc repetitive tasks .

One of the most important things to understand about functions is that they execute in the caller's process. Qshell begins a new process when it runs a script or external utility, but it does not start a new process when it executes a function. This means that functions are more efficient than scripts, and that a function can change its caller's variables directly.



Function Syntax

There are two acceptable ways to define a function. The older way, taken from the Bourne shell, works on all releases of Qshell. Here is the syntax:


function-name

() {



commands

}

The function name is followed by empty parentheses, which serve no purpose except to tell Qshell that a function is being defined. Next is an open brace, which may be on the same line or on the following line. The commands follow. They may follow the brace on the same line or the next line. The function is terminated with a closing brace on a line by itself.

The newer way to define a shell, introduced with V5R2, is like the function definition structure of the Korn shell. Its syntax is shown here:


function


function-name

{



commands

}

This syntax differs from the older syntax in two ways: The function begins with the reserved word function , and the empty parentheses are not coded.

A function must be defined before it can be invoked. For this reason, it is customary to place function definitions at the beginning of scripts.

In Figure 13.1, the user has typed the definition of the lcname (Lowercase Name) function into an interactive session. Since the function was defined in this session, it may be invoked from the command line. Note that the entire function has been coded on one input line. Since the closing brace is required to be on a line by itself, a semicolon is necessary to separate it from the rest of the function definition.

start figure


function lcname { echo   tr '[:upper:]' '[:lower:]' ; }


/home/jsmith $


filename=/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MYMBR.MBR


/home/jsmith $


lcname $filename


/qsys.lib/mylib.lib/myfile.file/mymbr.mbr

end figure

Figure 13.1: The lcname function has one compound command, which uses the tr (translate) utility to convert uppercase letters to the corresponding lowercase ones.

Here is the same function defined with the older syntax:


lcname() { echo   tr '[:upper:]' '[:lower:]' ; }