Forum Discussion
Andrea_Arquint
Nimbostratus
Jun 17, 2013using variable in a switch statement
Hi,
I would like to test a URI which is defined as a variable ($TEST_VAR):
when HTTP_REQUEST {
set TEST_VAR "item"
switch -glob [string tolower [HTTP::path]] {
"/$TEST_VAR/*" {
log local0. "Matched pool 2 paths for [HTTP::uri]"
pool pool2
}
default {
log local0. "Hit default for [HTTP::uri]"
pool pool_default
}
}
}
But this does not work.
How can this be done? Is such thing possible somehow?
Thanx
bb
5 Replies
- spark_86682Historic F5 AccountThis is possible. In Tcl, curly braces prevent variable substitution, and you have a set of curly braces surrounding the entire body of your switch statement, so "/$TEST_VAR/*" won't have its variable substituted. You should be able to fix this just by removing those outer braces around your switch body:
switch -glob [string tolower [HTTP::path]] "/$TEST_VAR/*" { log local0. "Matched pool 2 paths for [HTTP::uri]" pool pool2 } default { log local0. "Hit default for [HTTP::uri]" pool pool_default } - Andrea_Arquint
Nimbostratus
hi
I did that. Removed the curly braces as you showed but I could not save the irule within irule editor.
irule editor message: Response is not well-formed XML.So, Ithink this cannot be done at the moment with irules.
greetins
- hoolio
Cirrostratus
If you add a \ at the end of the first line of the switch statement it should work. Here are two forms of the syntax which allow for variables in the switch cases:when RULE_INIT { set case1 "test1" set case2 "test2" Syntax version 1 switch -glob -- "test2string1" $case1 { log local0. "matched exact \$case1: $case1" } $case2 { log local0. "matched exact \$case2: $case2" } $case2* { log local0. "matched starts_with \$case2: $case2" } default { log local0. "matched default case" } Syntax version 2 switch -glob -- "test2string2" \ $case1 { log local0. "matched exact \$case1: $case1" } \ $case2 { log local0. "matched exact \$case2: $case2" } \ $case2* { log local0. "matched starts_with \$case2: $case2" } \ default { log local0. "matched default case" } }
Aaron - Andrea_Arquint
Nimbostratus
that works
thanx aaron - hoolio
Cirrostratus
Summarized on the switch wiki page in the last two examples... https://devcentral.f5.com/wiki/iRules.switch.ashx
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects