Forum Discussion
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 = 😞
set var1 $var2 && !$var3
error: [wrong args][set var1 $var2 && !$var3]
set var1 ($var2 && !$var3)
error: [wrong args][set var1 ($var2 && !$var3)]
- Kevin_StewartEmployee
Try enclosing your Boolean expression in an expr command:
set var1 [expr var2 && !var3]
- Kevin_StewartEmployee
apparently the
syntax is not necessary here ?$varname
Yes it is required.
set var1 [expr $var2 && !$var3]
apparently the
evaluation is not necessary inside an[expr]
construct ?if {}
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.
- Kevin_StewartEmployee
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:
- nitass_89166Noctilucent
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 ?
- nitassEmployee
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 ?
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 ?
- Kevin_StewartEmployee
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.
- nitassEmployee
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 """
- Kevin_StewartEmployee
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.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com