Forum Discussion

uni's avatar
uni
Icon for Altostratus rankAltostratus
Sep 13, 2007

Preventing interpreting variable as TCL

I have an iRule which is parsing the each field in a URI query. I am getting an occasional problem with a TCL error if $parameter starts with "-", as the interpreter assumes it is an option to the split command. Is there some sort of quoting I can use to prevent this?

switch [string tolower [lindex [split $parameter "="] 0]] {

3 Replies

  • Can you add logging and reply with the TCL error? It seemed to work as expected in a simple test with a parameter and value that start with a hypher (or two hyphens):

    
    when RULE_INIT {
       set param_value "-par1=-val1"
       log local0. "\$param_value: $param_value"
       
       log local0. "\[split $param_value =\]: [split $param_value =]"
       log local0. "\[lindex \[split $param_value =\] 0\]: [lindex [split $param_value =] 0]"
    }

    Output:

    Rule : $param_value: -par1=-val1

    Rule : [split -par1=-val1 =]: -par1 -val1

    Rule : [lindex [split -par1=-val1 =] 0]: -par1

    Aaron
  • To include dashes in your variables, you can enclose the variable in braces and will tell the interpretor to treat the enclosed value as a single entity.

    switch [string tolower [lindex [split ${parameter} "="] 0]] {

    -Joe
  • uni's avatar
    uni
    Icon for Altostratus rankAltostratus
    For anyone who cares, the solution to this was to add "--" after the switch statement, to ensure it parses the next item as the test value rather than one of the command switches:

    switch -- [string tolower [lindex [split ${parameter} "="] 0]] {

    hoolio's test didn't pick it up, because it was a problem with the switch command syntax, which he didn't use in his test.

    Joe's suggestion is not correct: the {curly braces} protect the variable name, not the variable contents.

    Thanks for your suggestions.