Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

exec

tcl

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?

like image 885
Narek Avatar asked Sep 26 '12 15:09

Narek


People also ask

What is exec command in Tcl?

exec treats its arguments as the names and arguments for a set of programs to run. If the first args start with a "-" , then they are treated as switches to the exec command, instead of being invoked as subprocesses or subprocess options. switches are: -keepnewline. Retains a trailing newline in the pipeline's output.

How do I run a TCL command in Linux?

Instead Tcl reads it and places anything written into a variable unless you use one of the redirection options given in the manual page. For processes that produce a lot of output using open can be a better option as you can then read the output in pieces but for this example exec is fine.

Which command creates a function in Tcl?

[proc] creates a Tcl script based procedure (function) which can be defined to have a fixed or variable number arguments, with default values for the fixed arguments.


1 Answers

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)
} 
like image 86
slackwing Avatar answered Sep 18 '22 01:09

slackwing