uCon HomePage

Script Cmd: SET


SET [-efh] [ {VAR} [VAL] | [=EXPRESSION]  

Establish the content of a shell variable. This can be a simple equality (set VAR VAL) or it can be an expression (set VAR=$VAL+1).

As of Aug 2014, the SET command supports floating point operations by using -f option.  See example and note4 below.

If no arguments are specified, set will simply dump the current environment to the console similar to the menu item Config->Environment->Display.

To remove a variable (VAR) from the environment, use "SET VAR" (with no value specified).

Options:

-e    export (see note below) the variable being established.
-f    use floating point (double) as the base type within the expression (default is long).
-h    variable will be hidded from view.

The expression evaluator handles the following operators in the following priority:

1) Anything enclosed in ()'s as well as variables and functions.
2) Unary + and - signs
3) "To the power of" symbol: ^
4) Times (*), divide (/), and modulus (%)
5) Plus (+) and minus (-)
6) Assignments (=)

The evaluator supports logical functions for shifting and masking (|,&,>,<).

It also supports embedded functions. The current set of functions are:

-hex (VALUE) to convert a value to hex (format: "0x%x")
-hex0(VALUE) to convert a value to hex (format: "%x")
-hex2(VALUE) to convert a value to hex (format: "%02x")
-hex4(VALUE) to convert a value to hex (format: "%04x")
-hex8(VALUE) to convert a value to hex (format: "%08x")
-rand()      to generate a random number (seeded initially with time())
-abs(
VALUE)  to convert to the absolute value of the number.

The result is displayed in decimal unless somewhere within the expression the 'hex' function is used.

Examples:

- set VAR=99+5
- set VAR=hex(4000)
- set VAR=(99+$VAR)*4
- set VAR=0x1000>1
- set RAND=rand()
- set RAND10=rand()%10

Floating point examples (note the use of '-f'):

- set -f FVAL=99/5
  (FVAL will be 19.800000)
- set -f RVAL=round($FVAL)
  (RVAL will be 20.000000)
- set -f TVAL=trunc($RVAL)
  (TVAL will be 20)

Note1: The expression must contain no whitespace.

Note2: Under the context of uCon, an exported shell variable means it will be saved to a configuration file if a save is done after being exported. This is convenient for cases where environment variables are configured on the first pass of a script, then on later invocations of uCon that same environment can be restored if the variables are exported.

Note3: A 'hidden' variable simply means that it will not be displayed by an environment dump issued through Config->Environment->Display.  It will be displayed by the echo command, but this is up to the script.

Note4: As a result of the new -f option to support expressions with floating point, the 'IF' script command  also supports a '-f' option to allow it to correctly process arguments.