How to get the results (standard output) of a TCL exec command?

Narek picture Narek · Sep 26, 2012 · Viewed 25.3k times · Source

Say I have a TCL script like this:

exec ls -l 

Now this will print out the content of current directory. I need to take that output as a string and parse it. How I can do this?

Answer

slackwing picture slackwing · Sep 26, 2012

exec returns the output so simply set a variable to it:

set result [exec ls -l]

You may want to wrap this in a catch however:

if {[catch {exec ls -l} result] == 0} { 
    # ...
} else { 
    # ... (error)
}