SSH Client for Windows
Home Download Purchase Support Manual Screenshots Media About

4.2 Variables and Arithmetic Expressions

Private Shell Script engine allows using variables and calculating arithmetical expressions. Variables receive their values with the specific script action Set value (it is described below) and can be used in any script action.

Variables are referenced using the following syntax:

$varname or ${varname}

This syntax is allowed to appear anywhere in a text string, e.g. if you have a variable Friend with the value Bill, the string Hello, ${Friend}! will result in Hello, Bill!. Please note that variable substitution occurs only if you check the Preprocess parameters strings option for the current script step.

Arithmetical expressions use the following syntax:

${{expression}}

For example: ${{1 + 2*3}}, ${{5*sin (sqrt (2)/2)}}

It is possible to use variables in the calculations (if they contain real numbers), for example:

${{$base + $index*86.5}}

The following operations and mathematical functions are allowed in an arithmetical expression:

  • Real numbers (any number that looks like INTEGER or INTEGER.FRACTIONAL or INTEGER,FRACTIONAL, with an optional negative sign)
  • Private Shell variables (prefixed with dollar sign, e.g. $varname)
  • Addition ( + ), subtraction ( - ), multiplication ( * ) and division ( / )
  • Logical AND (&), OR (|) and XOR (^) operators
  • Boolean AND and OR operators (for conditions)
  • Comparative operators (= > < <> <= >=)
  • Parentheses
  • Mathematical functions (see the list below)

Logical operators treat operands as 32-bit integers and perform the following operations:

  • & - performs logical AND operation (i.e. the resulting bit is 1 if both corresponding source bits are 1). Example: 10 & 6 = 2,
  • | - performs logical OR operation (i.e. the result bit is 1 if any of the corresponding source bits is 1). Example: 10 | 6 = 14,
  • ^ - performs logical XOR operation (i.e. the result bit is 1 if only one of the corresponding source bits is 1). Example: 10 ^ 6 = 12.

Boolean operators (AND, OR) differ from logical ones in that they treat operands as 1-bit integers (0 is treated as 0, any other number as 1) and are designed to be used in conditional expressions. Examples: 10 AND 4 = 1, 10 AND 5 = 1, 10 AND 0 = 0.

Comparative operators return 1 if the condition is true for the given operator, and 0 otherwise, e.g.: 10 > 5 = 0, 10 <= 11 = 1.

Private Shell script engine supports the following mathematical functions:

  • abs (x) - absolute value of x,
  • sin (x) - sine of x,
  • cos (x) - cosine of x,
  • tan (x) - tangent of x,
  • exp (x) - exponential of x,
  • sqrt (x) - square root of x,
  • asin (x) - arcsine of x,
  • acos (x) - arccosine of x,
  • atan (x) - arctangent of x.

Warning: arithmetical expressions cannot be nested.

When a text string is preprocessed, all arithmetical expressions are calculated first, so the string Hello, ${var${{1 + 4}}} results in Hello, ${var5}, and then a variable substitution for the var5 variable occurs.


Comment On This Topic: