Advanced script programming-WC
 ***ing FAQs 
 ***ing < BACK TO QUESTIONS 
question
Advanced script programming
  
answer

 

POWERTERM SCRIPT RULES
  • PowerTerm Script language uses many `{` and not many `(`
  • NO: if ( $variable == "hello" )
    YES: if { $variable == "hello" }
  • Variable assignment does not use a `$`. Variable reference does.
  • NO: $var = $data
    YES: var = $data
  • assignment `=` must be surrounded by whitespace.
    (tests need not be surrounded by whitespace -- `if {$var<=10}` is acceptable.)

    NO: i=1
    YES: i = 1

  • The `{` starting a block of statements MUST be on the same line as the `if`, `for` ... statement.
  • YES: if { $data == 10 } {
    i = 45
    }

    NO: if { $data == 10 }
    {
    i = 45
    }

  • incrementing is interesting...
  • use the `expr` command which evaluates an expression, and the `[ ...]` facility which replaces itself with the result.

    Thus, i = [ expr $i + 1 ]
    or, use the `incr` command which increments its argument (and because there is an assignment involved, no `$` is used.
    i = [ incr i ]
    .. or ..
    i = [incr i 3 ] # increment by 3

  • `for` loop syntax is tricky:
  • as usual, there are 4 parts
    • initialization
    • test
    • increment
    • body statements
    ALL 4 PARTS ARE individually wrapped in `{...}`
    AND -- as indicated above -- the `{` beginning the body statements MUST be on the same line as the 3 earlier parts:
    NO: for (i=0; $i <10; $i++)
    var = 45

    YES: for {i = 0} {$i <10} { i="[incr" i]} {
    var = 45
    }

  • Testing for Time Outs in `wait` Here is an illustration of a script which uses a wait statement and an if to test the result:
  • str = [wait 10 for "hi there"]
    if {$str == "1"} {message "Hello to you"} else {message "no one home, $str"}

    For an unconditional wait, the `wait` command can be used in this manner:

    wait 10 seconds # [always say `seconds`, even in `wait 1 seconds`]
  • `wait`-ing for a string in a designated location The wait command can look for a string in a designated location, as in:
  • wait 10 for "F1" at 23 70

    This will wait 10 seconds for the string "F1" to appear in row 23, column 70.

  • Message and input boxes

    PowerTerm lets the script writer create pop-up boxes for 3 purposes: messages, text input and password-type input.

    • Message box.
      syntax: message text [title-text] [error|question|info]

      This will popup a box whose optional title is title-text, with the content of text, and with an optional icon suitable for error, question, or info. The user presses the OK button to continue.

    • Text input box.
      syntax: input-line text [title-text]

      This will popup a box which includes a text input area. The box has text of text, and an optional title of title-text. You can assign the input to a variable as in:

      data = [input-line "Enter Name" "My Title"]

    • password-type input box.
      syntax: input-password text [title-text]

      This command is similar to input-line, except that the text which the user inputs is not echoed back to the screen. Rather, `*` symbols are displayed.

  • Sending Hex Characters. This is done via `/xnnn`. example:
    key f3 do {send "\x301"}
  • Sending the Null `0` Character. This is done with the special character <null>. example: you need to send `ABCZDEF` where `Z` means the NULL `0` character. key f7 do {send "ABC<null>DEF"}
  • Sending the `Enter` key: for Unix/VMS, `^M` represents the Enter/Return key. For IBM Mainframe/Midrange, <enter> is the way to do it.
  • To include a filename in a script, it is necessary to use 2 backslash characters to represent each backslash. example: to refer to the file \tmp\myfile.txt , say \\tmp\\myfile.txt
  • To access a PC environment variable within a script, use the `GETENV` command. Here is an example:
  • data = [getenv TMP]
    if {$data == "bernie"} {message "yes"} else {message "no"}
  • To learn the current cursor position:
  • get col is the command for the column
    get row is the command for the row
  • To make a script `pause` after it executes a PC program:
  • The problem is that lines like:
    ...
    ...
    result = [exec MYPROG.EXE]
    if {$result == 0} {return}
    message "The Program failed" ERROR
    ...
    ...

    Will not behave as expected because the script will execute the `if {$result == 0} {return}` line immediately after starting MYPROG.EXE

    The solution is to have the script create a temp file, and to modify MYPROG.EXE to delete this file as its last operation.

    Then the script can be re-written as follows:

    ....
    ...
    open "\\temp\\temp.dat" w
    result = [exec MYPROG.EXE] #This will erase \temp\fred.dat when it completes
    while { 1 == 1 }

    wait 1 seconds
    isfile = [file exists "\\temp\\temp.dat"]
    if {$isfile == 0} {break}
    }
    if {$result == 0} {return} ##the files does NOT exist
    message "The Program failed" ERROR
    ...
    ...
  • To use the proc script command:
  • The proc must be defined before it is used [remember, the script language is an interpreter.] Here is an example of usage of proc.
    proc say_hello x
    message $x
    }

    say_hello Josephine

  • To exit PowerTerm under control of a script:
  • exit-emulator is the command



Top