Qshell predefines several variables, which it uses for two purposes:
To pass session settings to scripts
To store session information
Table 5.4 lists some of the predefined variables. They are covered in detail in later chapters.
|
Variable |
Description |
|---|---|
|
JOBNAME |
Qualified job
|
|
LINENO |
Line number |
|
OLDPWD |
Previous working directory |
|
PWD |
Working directory |
|
RANDOM |
Random number generator |
|
UID |
|
|
CDPATH |
Search
|
|
ENV |
Environment file |
|
HOME |
Home directory |
|
IFS |
Internal field separators |
|
PATH |
Search path for commands |
|
PS1 |
Primary prompt string |
|
PS2 |
Secondary prompt string |
Even though you do not declare these variables, you can use them as you would your own. Normally, you retrieve the values of predefined variables, but you do not change them. Figure 5.23 illustrates the use of the $RANDOM predefined variable.
|
|
i=0 ; while [ $i -lt 9 ] ; do print $RANDOM ; let "i=i+1" ; done 2656 684 13708 49702 58934 31462 46746 21854 48842
|
|
In Figure 5.24, the $HOME predefined variable is used. The user is working from the /home/JSMITH directory. The first cd command changes to source physical file SRC in library MYLIB. The cp command copies source member MATH to an IFS file in the user's home directory. The second cd changes back to the home directory. The ls command verifies that the copy was successful.
|
|
/home/JSMITH $ cd /qsys.lib/mylib.lib/src.file /qsys.lib/mylib.lib/src.file $ cp MATH.MBR $HOME /qsys.lib/mylib.lib/src.file $ cd ~ /home/JSMITH $ ls M* MATH.MBR MkXYZ.java /home/JSMITH $
|
|
You may define parameters and
Positional parameters are accessed by an ordinal number.
Predefined parameters are those to which the system
There are also two types of variables: those whose names are
already defined to Qshell, and those whose
You saw in chapter 5 that, when Qshell finds a dollar sign
Both the terms
parameter expansion
and
variable
expansion
are used in Unix literature to refer to this process,
no matter whether parameters or variables are being discussed. This
chapter uses the
In its simplest form, variable expansion returns the value
assigned to a parameter or variable. For example, $1 and ${1}
expand to the value of the first positional parameter, and $dirname
and ${dirname} expand to the value of a variable whose name is
Table 6.1 lists the four expansion operators. Use these
operators within the brace form of a variable or parameter. Before
the closing
|
Operator |
Description |
|---|---|
|
-(hyphen) |
Temporarily substitute for a missing value. |
|
+ |
Temporarily substitute for an existing value. |
|
= |
Assign a new value for missing value. (Not permitted with positional parameters.) |
|
? |
Generate an error if a value is missing. |
The term missing value in Table 6.1 needs some explanation. You might remember from chapter 5 that there is a difference between a variable that is unset and a variable that has a null value. An unset variable is not defined, while a variable with a null value is defined, but empty.
If the operators in Table 6.1 are preceded by a
$ var indicates that Qshell returns the existing value of the variable.
Value indicates that Qshell returns the value following the operator.
Assign value to var means that the value following the equal sign is assigned to the variable.
Null indicates that Qshell uses a null value in place of the variable reference.
Error
means that Qshell displays the message following
the question mark, or a default message, and exits a
|
Operator |
Var Is Defined; Not Null |
Var Is Null |
Var Is Unset ( Undefined ) |
|---|---|---|---|
|
${var:-value} |
$var |
value |
value |
|
${var-value} |
$var |
null |
value |
|
${var:=value} |
$var |
assign value to var |
assign value to var |
|
${var=value} |
$var |
null |
assign value to var |
|
${var:?[message]} |
$var |
error |
error |
|
${var?[message]} |
$var |
null |
error |
|
${var:+value} |
substitute value |
null |
null |
|
${var+value} |
substitute value |
substitute value |
null |
The
Use the hyphen operator to temporarily substitute a value when a variable has no value. For example, in Figure 6.1, the variable name is undefined (unset), so the first print command produces a blank line. The second print command prints the value Joe Smith because name has no value. However, the second print does not assign a value to the name variable, as the third print proves. Once the value Jack Sprat is assigned to name , the value following the hyphen is not used.
|
|
print $name
/home/JSMITH $
print ${name:-Joe Smith}
Joe Smith
/home/JSMITH $
print $name
/home/JSMITH $
name='Jack Sprat'
/home/JSMITH $
print ${name:-Joe Smith}
Jack Sprat
|
|
Figure 6.2 provides another example of the hyphen operator. In this case, the first print statement prints the value Bob because the variable name is undefined (unset). The second print does not print Bob because no colon precedes the hyphen operator, and name is null, but not unset.
|
|
print ${name-Bob}
Bob
/home/JSMITH $
name=
/home/JSMITH $
print ${name-Bob}
/home/JSMITH $
|
|
Qshell also provides a way to temporarily override the existing value of a variable. If the variable has a non-null value, the plus operator substitutes an alternate value. Otherwise, the result of the expansion is a null value.
Consider Figure 6.3. The first two print commands print blank lines because name is undefined . The third one prints Bubba even though name has the value Bill . The last print shows that the plus operator did not change the value of variable name .
|
|
print $name
/home/JSMITH $
print ${name+Bubba}
/home/JSMITH $
name=Bill
/home/JSMITH $
print ${name+Bubba}
Bubba
/home/JSMITH $
print $name
Bill
|
|
Use the equal-sign operator to assign a new value to a variable with no value. That is, the variable continues to have the new value after the expansion. This type of expansion is not allowed for positional parameters.
Figure 6.4 illustrates the equal-sign operator. The variable
name
is unset, so the first
print
command
|
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
print $name
/home/JSMITH $
print ${name:=Suzy Q}
Suzy Q
/home/JSMITH $
print $name
Suzy Q
|
|
The question-mark operator causes Qshell to issue an error message if a variable has no value. For example, the first three print commands in Figure 6.5 send messages to stderr because name is unset. The last print command does not send an error message because name has a value.
|
|
print ${name?}
qsh: 001-0021 Parameter 1$.*s is not set.
/home/JSMITH $
print ${name:?}
qsh: 001-0022 Parameter 1$.*s is not set or is null.
/home/JSMITH $
print ${name:?Name is
undefined
}
Name is
undefined
/home/JSMITH $
name='Larry, Curly and Moe'
/home/JSMITH $
print ${name:?Name is
undefined
}
Larry, Curly and Moe
|
|