correct syntax for boolean expressions

I experiencing errors with forming a simple boolean expression that would be valid in most “C like” languages ( if we added an equals sign = :disappointed_face:

set var1 $var2 && !$var3

error: [wrong  args][set var1 $var2 && !$var3]

set var1 ($var2 && !$var3)

error: [wrong  args][set var1 ($var2 && !$var3)]

Try enclosing your Boolean expression in an expr command:

set var1 [expr var2 && !var3]

apparently the

$varname

syntax is not necessary here ?

Yes it is required.

set var1 [expr $var2 && !$var3]

apparently the

[expr]

evaluation is not necessary inside an

if {}

construct ?

It’s possible that you can get away with not using expr in some conditions, but it’s semantically correct to use expr inside the if clause.

My hunch here would be that only one expr is needed. Also, and I missed this earlier, it’s more semantically correct to enclose the expressions in curly braces:

if { [expr {$v1 && $v2}] } { do something }

For a more detailed explanation of the expr syntax, I’d consult the tcl mothership:

http://www.tcl.tk/man/tcl8.5/TclCmd/expr.htm

e.g.

% set param1 ""
% set param2 "null"
null
% put [expr { ($param1 ne "") || ($param2 ne "null") }]
0

% set param1 "test"
test
% set param2 "test"
test
% put [expr { ($param1 ne "") || ($param2 ne "null") }]
1

e.g.

% set param1 ""
% set param2 "null"
null
% put [expr { ($param1 ne "") || ($param2 ne "null") }]
0

% set param1 "test"
test
% set param2 "test"
test
% put [expr { ($param1 ne "") || ($param2 ne "null") }]
1

Thanks nitass. What CLI are you using to execute interactive TCL ?

Thanks nitass. What CLI are you using to execute interactive TCL ?

I am not having good luck with a single expr and a complex boolean condition :

set var_1 [expr {($var_2 ne "") || ($var_3 ne null)}]
error: [parse error: PARSE syntax 489 {syntax error in expression "set var_1 [expr {($var_2 ne "") || ($var_3 ne null)}]": variable references require preceding $}][{($var_2 ne "") || ($var_3 ne null)}]

Could the problem here be that I am using variable names that contain underscores ?

Try this:

set var_1 [expr { [info exists var_2] || [info exists var_3] }]

By the way, Nitass is probably using the tclsh shell in the BIG-IP management GUI. You can also write pure TCL scripts with this:

!/usr/bin/tclsh

set var_2 true
set var_3 false

puts [info exists var_2]
puts [info exists var_3]

set var_1 [expr { [info exists var_2] || [info exists var_3] }]

puts $var_1

Then chmod the script:

chmod 755 balto.tcl

And run it like any other Bash script:

./balto.tcl

Great for troubleshooting TCL logic outside of an iRule.

e.g.

1

[root@ve11a:Active:In Sync] config  tmsh list ltm rule qux
ltm rule qux {
    when RULE_INIT {
  set var_2 ""
  set var_3 "null"
  set var_1 [expr {[expr {$var_2 ne ""}] || [expr {$var_3 ne "null"}]}]
  log local0. $var_1
}
}
[root@ve11a:Active:In Sync] config  cat /var/log/ltm
Jul  5 21:36:55 ve11a info tmm[29362]: Rule /Common/qux : 0
Jul  5 21:36:55 ve11a info tmm1[29362]: Rule /Common/qux : 0

2

[root@ve11a:Active:In Sync] config  tmsh list ltm rule qux
ltm rule qux {
    when RULE_INIT {
  set var_2 "test"
  set var_3 "test"
  set var_1 [expr {[expr {$var_2 ne ""}] || [expr {$var_3 ne "null"}]}]
  log local0. $var_1
}
}
[root@ve11a:Active:In Sync] config  cat /var/log/ltm
Jul  5 21:38:55 ve11a info tmm[29362]: Rule /Common/qux : 1
Jul  5 21:38:55 ve11a info tmm1[29362]: Rule /Common/qux : 1

After a lot of trial-and-error, I finally found a syntax that was accepted by BIG-IP iRule editor :

if { [expr {[expr $var1 ne ""] || [expr $var2 ne null]}] } {
  set var3 1
}

However I receive runtime error :

01220001:3: TCL error: /Common/my-irule.conf  - syntax error in expression " ne ": unexpected operator ne     while executing "expr $var1 ne """
if { [expr { [expr { $var1 ne "" }] || [expr { $var2 ne "" }] } ] }

I don’t believe null is a keyword in TCL, so the [info exists] syntax is probably a better option.

null nil undefined do not exist in tcl

However I am receiving error when applying string operator

ne

to param and cookie values :

set param1 [URI::query [HTTP::uri] param1]
set cookie1 [HTTP::cookie value "cookie1"]

if { [expr { [expr { $param1 ne "" }] || [expr { $cookie1 ne "" }] } ] }
TCL error: /Common/myirule.conf  - syntax error in expression " ne ": unexpected operator ne     while executing "expr $param1 ne """