Forum Discussion

Aurel's avatar
Aurel
Icon for Cirrus rankCirrus
Dec 02, 2013

Irule syntax issue : missing a script

Hi, I'm having the error message on line 18 "missing a script". What i'm trying to do with this iRule is to append one or two "0" when the $fract value contains only two or one digit, whereas most of the time is has 3 digits.

Any help so much welcome.

when HTTP_REQUEST {

set secs [clock seconds]    
set msec [clock clicks -milliseconds]   
set base [expr { $secs * 1000 } ]   
set fract [expr { $msec - $base }]  

log local0. "step_1 $fract"

if {$fract >= 1000 } { set diff [expr { $fract / 1000 }] incr secs $diff incr fract [expr { -1000 * $diff }]}

log local0. "step_2 $fract"

if {[$fract matches_regex "\d\d"]} append fract 0 elseif {$fract matches_regex {/\d}} append fract 00

log local0. "Timestamp with milliseconds is $secs.$fract" }

9 Replies

  • It's a problem on formatting.

    After each if condition you have to add curly brackets.

    if { your condition } {
        What_you_want_to_do
    }
    else { other_condition } {
        actions
    }
    
    • Aurel's avatar
      Aurel
      Icon for Cirrus rankCirrus
      That's it Thomas. I forgot the curly braces for the "What i want to do". A big thank you ! Strange that the irule editor said nothing about it, yet he's quite talkative usually.
  • It's a problem on formatting.

    After each if condition you have to add curly brackets.

    if { your condition } {
        What_you_want_to_do
    }
    else { other_condition } {
        actions
    }
    
    • Aurel's avatar
      Aurel
      Icon for Cirrus rankCirrus
      That's it Thomas. I forgot the curly braces for the "What i want to do". A big thank you ! Strange that the irule editor said nothing about it, yet he's quite talkative usually.
  • I mean with you code :

     

    if {[$fract matches_regex "\d\d"]} {  
        append fract 0  
    } elseif {$fract matches_regex {/\d} } {  
        append fract 00
    }
  •  I just test the syntax and it works.
     Remember: 
     Trim the end of each line, indent the beginning for best pratices 
     and write the conditions with curly braces.
    
     To paste the code on this forum, start each line with {tab} character
    
    when HTTP_REQUEST {
    
        set secs [clock seconds]
        set msec [clock clicks -milliseconds]
        set base [expr { $secs * 1000 } ]
        set fract [expr { $msec - $base }]
    
        log local0. "step_1 $fract"
    
        if {$fract >= 1000 } {
            set diff [expr { $fract / 1000 }]
            incr secs $diff
            incr fract [expr { -1000 * $diff }]
        }
    
        log local0. "step_2 $fract"
    
        if {[$fract matches_regex "\d\d"]} {
            append fract 0
        }
        elseif {$fract matches_regex {/\d} } {
            append fract 00
        }
    
        log local0. "Timestamp with milliseconds is $secs.$fract"
    }
  • I even found a way to do the job i wanted within one single line : set timestamp [format "%d%03d000" $timestamp $fract]

     

    Thanks to a colleague who speaks fluently perl, tcl among others.