Flylib.com

Books Software

 
 
 

Predefined Variables


Predefined Variables

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.

Table 5.4: Some Predefined Qshell Variables

Variable

Description

JOBNAME

Qualified job name

LINENO

Line number

OLDPWD

Previous working directory

PWD

Working directory

RANDOM

Random number generator

UID

User identifier

CDPATH

Search path for cd

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.

start figure


i=0 ; while [ $i -lt 9 ] ; do print $RANDOM ; let "i=i+1" ; done


2656


684


13708


49702


58934


31462


46746


21854


48842

end figure

Figure 5.23: Use $RANDOM to retrieve a random number.

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.

start figure


/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 $

end figure

Figure 5.24: Use $HOME to refer to the home directory.



Summary

You may define parameters and variables to hold data values. There are two types of parameters:

  • Positional parameters are accessed by an ordinal number.

  • Predefined parameters are those to which the system assigns values.

There are also two types of variables: those whose names are already defined to Qshell, and those whose names you must assign. Before V5R2, variables were not types. As of V5R2, you may assign a data type to a variable if you wish. You may also use the readonly utility to specify that a variable's value is unchangeable. You create a variable by assigning a value to it, or by declaring it with the declare or typeset utility.



Chapter 6: Using Parameters and Variables

You saw in chapter 5 that, when Qshell finds a dollar sign preceding a parameter or variable name , it replaces the reference with the value of the indicated parameter or variable. This is known as expanding the parameter or variable.

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 term variable expansion because all facets of expansion are supported with variables, but one type of expansion is not supported with positional parameters.

Variable Expansion

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 dirname . However, Qshell provides operators that permit more powerful methods of variable expansion.

Table 6.1 lists the four expansion operators. Use these operators within the brace form of a variable or parameter. Before the closing brace , include the operator, and follow it with another value, whose function depends on the operator.

Table 6.1: Conditional Expansion Operators

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 colon , the term missing value means "an unset variable or a variable with the null value." Without a preceding colon, the term missing value means "an unset variable." As a rule, you will probably want to include the colon, since your consideration when using these parameter operators is more likely to be whether or not a value is missing, not why it is missing. Table 6.2 illustrates this difference more closely. Keep the following in mind to help interpret the table:

  • $ 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 noninteractive shell.

Table 6.2: Results of Variable Expansion Operators

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 next several sections of this chapter examine each of the variable expansion operators in detail.

Temporary Substitution for a Missing Value

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.

start figure


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

end figure

Figure 6.1: The presence of a colon causes null and unset values to be treated equally.

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.

start figure


print ${name-Bob}


Bob


/home/JSMITH $


name=


/home/JSMITH $


print ${name-Bob}


/home/JSMITH $

end figure

Figure 6.2: This example illustrates the difference between using the hyphen operator with an unset variable and a null variable.

Temporary Substitution of an Existing Value

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 .

start figure


print $name


/home/JSMITH $


print ${name+Bubba}


/home/JSMITH $


name=Bill


/home/JSMITH $


print ${name+Bubba}


Bubba


/home/JSMITH $


print $name


Bill

end figure

Figure 6.3: These commands illustrate the plus operator.

Assigning a New Value for a Missing Value

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 leaves a blank line. The variable expansion in the second print assigns the value Suzy Q to name . The third print shows that name has kept its new value.

start figure

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}


print $name


/home/JSMITH $


print ${name:=Suzy Q}


Suzy Q


/home/JSMITH $


print $name


Suzy Q

end figure

Figure 6.4: The equal-sign operator assigns a new value to a variable with no value.

Generating an Error If a Value Is Missing

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.

start figure


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

end figure

Figure 6.5: These commands illustrate the question-mark operator.