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?
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.
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.
[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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With