Forum Discussion

Sayan_81841's avatar
Sayan_81841
Icon for Nimbostratus rankNimbostratus
Feb 24, 2011

Switch statement not working

Hi,

I wrote an iRule and the content is given below. But this is not working as expected.

 

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] starts_with "abc.com" } {

 

set var1 "/abc/xyz"

 

set var2 "/xyz/abc"

 

 

 

switch -glob [HTTP::uri] {

 

$var1 {

 

HTTP::redirect "http://www.google.com"

 

}

 

$var2 {

 

HTTP::redirect "http://www.yahoo.com"

 

}

 

concat $var1 "mail" {

 

HTTP::redirect "http://mail.yahoo.co.uk"

 

}

 

default {

 

}

 

}

 

}

 

}

 

 

 

 

The compilation is successful and no syntax error but the redirections are not happening and always showing "Page not found". But when I converted the above iRule to if elseif statement it started working.

 

 

 

Please let me know what is the issue with the iRule.

 

  • Hi Sayan,

    The curly braces at the beginning and end of the switch statement prevent variable expansion. See this thread for an alternative:

    http://stackoverflow.com/questions/3261515/tcl-switch-glob-does-not-match-with-variable

    
    when RULE_INIT {
    
    set var1 "test1"
    set var2 "test2"
    
    switch -glob -- "test1mail" $var1 {
    log local0. "matched $var1"
    } $var2 {
    log local0. "matched $var2"
    } "${var1}mail" {
    log local0. "matched ${var1}mail"
    } default {
    log local0. "no match"
    }
    
    }
    

    : matched test1mail

    Aaron
  • Yes, I came into that a while back when writing a tech tip. You can omit the curly braces and use a line continuation character to make the switch statement a single line. Otherwise, I haven't found any workaround to this.

     

     

    -Joe