Forum Discussion
dlg_23340
Nov 18, 2011Cirrus
variables in a switch statement
What are my options for variable expansion in switch cases? It seems to me that in the following code, when i visit http://example.com/bar/test, I should get "you found foo." for my page content. In practice, this isn't the case. What do I need to do to have the variable $foo expanded inside the switch?
Thanks.
when HTTP_REQUEST {
set foo "bar"
switch -glob -- [URI::decode [HTTP::uri]] {
"/$foo/*" { HTTP::respond 200 content "you found foo." }
}
}
- nitassEmployee
[root@ve1023:Active] config b virtual bar list virtual bar { snat automap pool foo destination 172.28.19.79:80 ip protocol 6 rules myrule profiles { http {} tcp {} } vlans external enable } [root@ve1023:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { set foo "bar" switch -glob -- [URI::decode [HTTP::uri]] " /$foo/* { HTTP::respond 200 content {you found foo.} } " } } [root@ve1023:Active] config curl -i http://172.28.19.79/bar/abc HTTP/1.0 200 OK Server: BigIP Connection: Keep-Alive Content-Length: 14 you found foo.
- nitassEmployeeanother one.
[root@ve1023:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { set foo "bar" switch -glob -- [URI::decode [HTTP::uri]] \ "/$foo/*" { HTTP::respond 200 content "you found foo." } } } [root@ve1023:Active] config curl -i http://172.28.19.79/bar/abc HTTP/1.0 200 OK Server: BigIP Connection: Keep-Alive Content-Length: 14 you found foo.
- smp_86112CirrostratusWhat the heck?!?! Now that I read the official TCL doc closely (http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm), the first example does explicitly state that variables can be used with the syntax used in your second example. But what's up with the first example - how did you come up with that one?
- nitassEmployeehow did you come up with that one?i remember brace and double quote is interchangeable (except variable substitution) but honestly i forgot where i remember the 1st example form. i might mix it up in my mind. it may not be indeed correct. sorry about that. ^ ^
- hoolioCirrostratusHere's one place we discussed it:
- DaveC_53879NimbostratusThanks Aaron.
- dlg_23340CirrusThanks, Nitass. I'm using your first example because I prefer its structure... But I'm running into a problem. I have code like this:
set foo "bar" switch -glob -- [URI::decode [HTTP::uri]] " /$foo/* { HTTP::respond 200 content {you found foo.} } default { set x {hello} log local0. {$x} } "
- nitassEmployeemaybe the 2nd example is better. can you try this?
[root@ve1023:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { set foo "bar" switch -glob -- [URI::decode [HTTP::uri]] \ "/$foo/*" { HTTP::respond 200 content "you found foo." } \ default { set x "hello" log local0. $x } } } [root@ve1023:Active] config curl -i http://172.28.19.79/bar/abc HTTP/1.0 200 OK Server: BigIP Connection: Keep-Alive Content-Length: 14 you found foo.
- hoolioCirrostratusI added these to the DC wiki page for switch:
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