Forum Discussion

Michael_Hunzike's avatar
Michael_Hunzike
Icon for Nimbostratus rankNimbostratus
Mar 11, 2005

Problem with elseif

Okay, this is driving me nuts...I'm trying to get a simple nested if statement working and it's not....

This works:

    
  when HTTP_REQUEST   
  {   
     if {[HTTP::host] equals "dev.domain.com" }   
     {   
        if { [HTTP::uri] starts_with "/wps/portal" }   
        {   
           log "matched /wps/portal"   
           pool dev_domain_portal   
        }   
        else   
        {   
           log "no match on [HTTP::uri] fallthrough sending to portal home"   
           HTTP::redirect "http://dev.domain.com/wps/portal/Home"    
        }   
     }    
  }   
  

When I add an elseif so that it looks like this:

    
  when HTTP_REQUEST   
  {   
     if {[HTTP::host] equals "dev.domain.com" }   
      {   
          if { [HTTP::uri] starts_with "/wps/portal" }   
            {   
            log "matched /wps/portal"   
            pool dev_domain_portal   
            }   
            else   
            {   
            log "no match on [HTTP::uri] fallthrough sending to portal home"   
            HTTP::redirect "http://dev.domain.com/wps/portal/Home"    
            }   
      }    
     elseif { [HTTP:host equals "dev.newdomain.com" }   
      {   
      discard   
      }   
      
  }   
  

I get this error:

line 16: [undefined procedure: elseif] [elseif { [HTTP:host equals "dev.newdomain.com"}

{

discard

}]

7 Replies

  • rapmaster_c_127's avatar
    rapmaster_c_127
    Historic F5 Account
    We're a little overly sensitive to whitespace. Also your HTTP::host line is wrong. Try this:

     
     rule rastak { 
        when HTTP_REQUEST { 
             if {[HTTP::host] equals "dev.domain.com" } { 
                 if { [HTTP::uri] starts_with "/wps/portal" } { 
                     log "matched /wps/portal" 
                     pool dev_domain_portal 
                 } else { 
                     log "no match on [HTTP::uri] fallthrough sending to portal home" 
                     HTTP::redirect "http://dev.domain.com/wps/portal/Home" 
                 } 
             } elseif { [HTTP::host] equals "dev.newdomain.com" } { 
                 discard 
             } 
         } 
     } 
      
     
  • First thanks to both of you for responding.

     

     

    Yea, the missing ] was a typo when recreating a shorter rule to illustrate the problem. It must have something to do with whitespace. This is a total pain. It seems sometimes whilespace is okay and sometimes it's not. Why is an elseif required to be on the same line as a closing brace sometimes and not others? In my first example (which works like a champ) I don't have any problems with else or elseif being on the next line. It only seems to manifest itself when I add an else to the outermost if statement. And in cutting and posting your response code it fails with the following whenj I hit "update" when entering the rule.

     

     

    line 14: [command is not valid in the current scope][}]

     

     

     

  • rapmaster_c_127's avatar
    rapmaster_c_127
    Historic F5 Account
    Hi,

     

     

    The whitespace issue with elseif is a bug. The original TCL specification is actually pretty whitespace-sensitive, so we relaxed the whitespace rules. We missed a spot though. We'll be fixing it for elseif in an upcoming release, but if you follow BSD indentation style for now (brace on the same line as the expression) you should be fine.

     

  • rapmaster_c_127's avatar
    rapmaster_c_127
    Historic F5 Account
    One quick question about your cutting and pasting my rule into the UI: are you making sure you omit the "rule rastak" line and closing brace? I pasted this from my bigip.conf. Putting rules into the UI requires that you omit the rule declarators.
  • Posted By rapmaster_c on 3/11/2005 9:54:00 AM

     

     

    One quick question about your cutting and pasting my rule into the UI: are you making sure you omit the "rule rastak" line and closing brace? I pasted this from my bigip.conf. Putting rules into the UI requires that you omit the rule declarators.

     

     

    Yup, I omitted that. I just tried to reformat my real rule to use the more traditional (and hard for me to read) style and without the last elseif it works fine. If I add the elseif on behind the second to the last } it still fails. I'll retry cutting and pasting your rule again to see if I missed something.

     

  • Okay, it's working. elseif after the 2nd to last } and on the same line with one space works.

     

     

    Anyway, thanks for the help. Your answer in the end was correct. Not sure why your code snippet was failing.
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account

     

    line 14: [command is not valid in the current scope][}]

     

     

    This error implies you had an extra close brace "}" at the outer most scope (the same indent level as the "when"). Perhaps when you selected the code and left off the opening "rule rastak {" you still picked up the closing brace.

     

     

    Generally, Tcl requires all commands arguments to be on the same line. However, the components of a list may be spread across multiple lines.

     

     

    We did not feel this syntax adequately allowed users that were used to 4.x rules to continue to use the indent styles they were used to in 4.x. So we modified the Tcl language to be more forgiving with the if command (and for the most part it is). However, when we made this modification we did not account for extra whitespace that might be at the end of the previous line. This is likely the issue you encountered with the elseif argument. We are fixing this, so please be patient.

     

     

    Additionally, we have been working hard on a GUI rule builder that will make writing some of the simpler rules far more easy.