1

我正在尝试执行具有一些选项的程序,并将其作为输入 txt 文件。所以我尝试了这个:

set myExecutable [file join $::env(path_to_the_program) bin executable_name] 
if { ![file exists $myExecutable ] } {
puts "error"
}

if { ![file executable $myExecutable ] } {
puts "error"
}


set arguments [list -option1 -option2]
set status [catch { exec $myExecutable $arguments  $txtFileName } output]
if { $status != 0 } {
    puts "output = $output"
}

所以它是打印:

output = Usage: executable_name -option1 -option2 <txt_file_name>
child process exited abnormally
4

1 回答 1

4

您实际上并没有向您的可执行文件提供参数。只是文本文件名。尝试:

set status [catch {exec $myExecutable -option1 -option2 $txtFileName} output]

或者,如果您更喜欢将参数保留在列表中:

set status [catch {exec $myExecutable {*}$arguments} output]

其中 {*} 语法将导致列表就地展开。在添加此之前的 Tcl 版本(8.5)中,您将使用:

set status [catch {eval exec [list $myExecutable] $arguments} output]

其中 eval 命令解包列表,以便 exec 看到一组参数。在 $myExecutable 周围添加额外的 [list] 语句可以保护其内容不被解释器通过视为列表。

于 2013-08-01T15:16:27.050 回答