Forum Discussion
Variable substitution in switch statement not working
I sent that last answer in something of a hurry and realized there are a few nuances that may further help.
The switch command searches the parameter list for any flag arguments (e.g., "-glob"), processes them, extracts the match condition, then looks to see how many parameters remain. If it is one, then it treats it the usual way, which means no case expansion. Remember from the discussion above that if the switch looks like this:
switch [HTTP::path] {
"/foo/bar" {
...
}
"/etc/etc/etc.html" {
...
}
}
then the switch command has two parameters; namely, the match condition ([HTTP::path]), then all of the contents of the squirly braces.
Alternatively, if, after removing the flags and the match condition, there is more than one parameter remaining (strictly speaking, there must be an even number), then it treats it in the second way; that is, it treats the parameters as a series of case+branch pairs and expands each of the cases.
This leads to the second subtlety. In general, if you use the second form, it's a good idea to put the flags first, then a double-dash, then the cases, like this:
switch -glob [HTTP::path] -- \
"$x/*" { \
log local0. "Matched x: [HTTP::path]" \
HTTP::path "/new_path.html" \
} \
"/foo/bar/*" - \
"/baz/bing.html" { \
... \
}
The double-dash instructs switch to stop looking for flags after that point. This is defensive. Let's say one of your cases was "-yadda". switch would panic because it wouldn't recognize this "flag". By using the double-dash (--) before that case, it wouldn't try to interpret "-yadda" as a flag, but rather, treat it as a case.
I know this is a very long answer to your very short question, but I hope that it is helpful.
Hi Vernon, Thanks for your much informative and elaborate answers. Appreciate it. I earlier tried to put the "\" at end of each case but gave me syntax error. your answer puts \ at the end of each line even inside the case. I am not sure if its feasible for me as I have some 200 lines of code in the switch statement, which includes another switch in one or more cases, and to keep \ at end of each line. I will try this out and see if that removes the syntax errors.
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