Using HP 3000 MPE/iX:\Advanced Skills Tutorial: HP 3000 MPE/iX Computer Systems > Chapter 7 Module 6: Variables and ExpressionsLesson 2 Using Variables and Expressions |
|
Lesson 2 presents the following information about how the variables can actually be used, once they have been defined:
Look at how the variables HPJOBLIMIT and HPJOBCOUNT are used in this command file.
ECHO displays whatever information follows it. ECHO is often used to display text messages on the screen. In the STATS file, one line that is to be displayed is:
The exclamation point will not be displayed, nor will the word "HPJOBCOUNT." Instead, the current value of HPJOBCOUNT will replace the word and appear on the screen. This indicates how many jobs are currently running. For example, suppose that there are 25 jobs executing on the system. The ECHO command STATS produces this message on the screen:
This is called variable dereferencing. Variable dereferencing refers to the act of substituting the value of a variable in place of the variable name before executing the command that contains that variable name. There are two ways to do this (examples appear on the following pages): implicit dereferencing and explicit dereferencing. This involves using an exclamation point(!). This method is similar to the method used with parameters of user commands. When the system encounters an exclamation point in front of a variable name, it substitutes the value for the variable name. This method is available with any command line. This does not involve an exclamation point. It is done automatically, without an exclamation point. Implicit deferencing occurs in the commands SETVAR, IF-THEN-ELSE-ENDIF, WHILE-ENDWHILE, and CALC. (You will look at IF-THEN-ELSE-ENDIF and WHILE-ENDWHILE later in this lesson.) The CALC command is a simple calculator command that will not be discussed; however, its syntax and description are in the online help facility and in the MPE/iX Commands Reference Manual Volumes 1 and 2 (32650-90003 and 32650-90364)
You have already seen examples of value assignment with SETVAR, like the following, where no exclamation point was necessary:
HPUSER requires no exclamation point. If you wanted to set your prompt so that it would echo your user name, as stored in HPUSER, you would have to use an exclamation point:
HPUSER does require an exclamation point in this case. An exclamation point is necessary if a variable name must be used within the quotation marks following SETVAR. Without the exclamation point, the new prompt looks like this:
Now look at another case where no exclamation point is necessary, the IF-THEN-ELSE statement. In an IF-THEN-ELSE statement, when the IF condition is true, the THEN action is performed; otherwise (if it is not true), the ELSE action is performed. In the STATS command file, a simple IF-THEN-ELSE statement is used to compare the job limit with the current number of jobs running in order to determine what action to take. This means that if the current number of jobs is getting close to the job limit, a message is displayed indicating that your job should be streamed later. Otherwise, a different message is displayed. ENDIF marks the end of the statement.
Variables in the IF-THEN-ELSE statement are used in conjunction with certain math operations. These operations are represented by symbols. A combination of variables, constants, and operators are referred to as an expression. Variables that appear in expressions do not require exclamation points (!) to be deferenced. Dereferencing is done implicitly:
The first symbol (<=) represents a comparative (relational) operation: less-than-or-equal-to. The second symbol (-) represents an arithmetic operation, subtraction. The following is a basic list of the operations that you can perform in MPE/iX variables, and the symbols that represent those operations. For a more exhaustive list, refer appendix B of the MPE/iX Commands Reference Manual (32650-60002). Arithmetic Operations:
Relational Operations:
Hints:
********** End of Exercise 6-2 ********** Let's look at how the variables RESPONSE, FILENAME, and OPTION are used in the LF command file:
Other commands may be used to assign a value to a variable. The INPUT command lets the user interactively assign or change a variable value. This command can optionally display a prompt on the screen, and the value that is entered by the user is assigned to the specified variable. For example, in order for the LF command file to operate correctly, it must ask the user whether or not it should list a file. INPUT specifies the variable (RESPONSE) whose value will be supplied by the user; the PROMPT keyword specifies the prompt that the user sees:
The user sees this:
If the PROMPT option were not specified, the user would only see a blank line and have to guess what to input. It makes more sense to prompt for input in an intelligent manner, by printing some type of message. INPUT treats all user responses as string values (series of alphanumeric characters). This means that if the user enters a digit, it will be considered a string value, not a numeric value. To use such digits from an INPUT statement as integers, simply dereference the variable explicitly, as shown below: Example: Suppose that you wish the user to enter a number so that an addition operation can be performed. Please create a small command file called ADD1:
Execute the ADD1 command file. It performs the addition because NUMBER is treated as an integer (2) since you explicitly dereferenced it as !NUMBER. Now create a new command file called ADD2. In it, deliberately do not explicitly dereference the number:
Execute the ADD2 command file. You get this error:
ADD2 does not perform the addition, since NUMBER is treated as a string value, not as a numeric value.
The WHILE loop is similar to the IF-THEN-ELSE-ENDIF statement in that it involves conditions and actions. As the name implies, the actions are performed over and over again (in a loop) until the conditions are no longer true.
As long as the condition is true, the action continues. As soon as the condition is no longer true, the action stops. Note the following about the WHILE-ENDWHILE syntax:
********** End of Exercise 6-3 **********
|