参见:
Here are some examples of the use of theexeccommand on Unix.
To execute a simple program and get its result:execuname -a
To execute a program that can return a non-zero result, you should wrap the call toexecin-errorcodereturn option if you have an error:set status 0
if {[catch {execgrep foo bar.txt} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
} else {
# Some kind of unexpected failure
}
}
When translating a command from a Unix shell invocation, care should be taken over the fact that single quote characters have no special significance to Tcl. Thus:awk '{sum += $1} END {print sum}' numbers.list
would be translated into something like:execawk {{sum += $1} END {print sum}} numbers.list
If you are converting invocations involving shell globbing, you should remember that Tcl does not handle globbing or expand things into multiple arguments by default. Instead you should write things like this:execls -l {*}[glob *.tcl]
WINDOWS EXAMPLES
Here are some examples of the use of theexeccommand on Windows.
To start an instance ofnotepadediting a file without waiting for the user to finish editing the file:execnotepad myfile.txt &
To print a text file usingnotepad:execnotepad /p myfile.txt
If a program calls other programs, such as is common with compilers, then you may need to resort to batch files to hide the console windows that sometimes pop up:execcmp.bat somefile.c -o somefile
With the filecmp.batlooking something like:@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
Sometimes you need to be careful, as different programs may have the same name and be in the path. It can then happen that typing a command at the DOS prompt findsa different programthan the same command run viaexec. This is because of the (documented) differences in behaviour betweenexecand DOS batch files.
When in doubt, use the commandexeccommand. This applies especially when you want to run “internal” commands likedirfrom a Tcl script. (if you just want to list filenames, use theexec{*}[auto_execok dir] *.tcl