Forum Discussion
BIG-IP : iRule test that variable exists and has certain value
F5 BIG-IP Virtual Edition v11.4.1 (Build 635.0) LTM on ESXi
What is the simplest most elegant syntax to test that a variable exists and has a certain value ?
For example if I want to determine that myvariable exists and has non-zero value , is this the simplest syntax ?
when HTTP_REQUEST {
set myvariable 0
conditional processing ...
set myvariable 1
more processing ...
if {[info exists $myvariable] && [$myvariable]} {
do something
}
- Michael_JenkinsCirrostratus
You'll need to use
(without the[info exists myvariable]
) to check for variable existence. Then you would do your other conditional check.$
So like this in your example
when HTTP_REQUEST { set myvariable 0 conditional processing ... set myvariable 1 more processing ... if {[info exists myvariable] && ($myvariable equals "1")} { do something } }
If you know that your variable is going to be an integer, you could just use
instead of$myvariable
as it will return a true value for any positive value.($myvariable equals "1")
Hi BaltoStar,
if the given variable exists in 90% of the cases, then
will do an awesome job...[catch {}]
when HTTP_REQUEST { if { [catch { if { $variable } then { log local0.debug "The variable does exist and has a value of 1" } }] } then { log local0.debug "The variable does not exists." } }
... but if the variable exists in less than 90% of the cases, then
will start to outperform[info exists]
.[catch {}]
when HTTP_REQUEST { if { ( [info exists variable] ) and ( $variable ) } then { log local0.debug "the variable does exist and has a value of 1" } else { log local0.debug "The variable does not exists or has an value other than 1." } }
Note: The reson for this is that a successful
command is slightly faster than[catch {}]
but on the other hand an unsuccessful[info exists]
command is much slower than[catch {}]
.[info exists]
Cheers, Kai
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