Forum Discussion

J_LE_42749's avatar
J_LE_42749
Icon for Nimbostratus rankNimbostratus
May 23, 2014

TCL & TMSH command > unexpected argument "|" while using grep

Hello World!

 

I am currently struggling with TCL & TMSH grep & need some advice.

 

I'm trying to automate APM object creation and for that I need at some point to list the existing ACLs in order to specify the ID of the new APM object ACL.

 

The TMSH command to do so is tmsh::list apm all-properties | grep acl-order. It works perfectly from CLI.

 

Now, I wanted to run this command within a TCL script... and here start my issue :) TCL does not like the pipe character.

The command: set acl_list [split [tmsh::list apm all-properties | grep acl-order] "\n"]

 

The output: SAG-webtop.tcl: script failed to complete: can't eval proc: "script::run" unexpected argument "|" while executing "tmsh::list apm all-properties | grep acl-order" (procedure "script::run" line 35) invoked from within "script::run" line:1 script did not successfully complete, status:1

 

I tried to add a \ before the | but then got the same output.: SAG-webtop.tcl: script failed to complete: can't eval proc: "script::run" unexpected argument "|" while executing "tmsh::list apm all-properties | grep acl-order" (procedure "script::run" line 35) invoked from within "script::run" line:1 script did not successfully complete, status:1

 

Any idea on how to get this working?

 

Many thanks Jérôme

 

  • Not at all the prettiest thing in the world, but try this:

    proc script::run {} {
        set order 0
        foreach x [split [tmsh::list apm all-properties] "\n"] {
            if { [string trimleft $x] starts_with "acl-order" } {
                set num [lindex [split [string trimleft $x]] 1]
                if { [expr { $num > $order }] } {
                    set order $num
                }
            }
        }
        puts "largest: $order"
    }
    

    This will loop through each line of the tmsh command's result, find acl-order and its value, and then return the largest number.

  • Thanks a bunch Kevin! I forgot to subscribe to the post so I just discover your input.

    Meanwhile I did the work with regex:

    proc script::run {} {
    ...
        set dump [tmsh::list apm all-properties]  
        set last_acl [lindex [lsort -integer [regexp -all -inline -- {\d+} \ 
           [regexp -all -inline -- {\yacl-order\y\s\d+} $dump] ]] end-1] 
        set cmd "$name acl-order $acl_id application-uri $uri description \"$desc\" "
        puts "\t$cmd"
    ...
    }
    

    NB: the 65535 ID for the ACL is reserved in our environment

    I am not sure this is the most sexy way to do that, however 🙂

    I'm also concerned about performances: is it better to use regexp(s) or go with loop as suggested by Kevin? I would say that regex eats more but looping through such a big output can also take a while.

    Any advice?

    Thanks Jérôme