如何让 'exec' 的结果显示在调用 shell 中?

How does one get the results of 'exec' to show in the invoking shell?

我使用 tcl gui 只是为了帮助创建有效的命令行,否则这些命令行太烦人而无法输入。我只是希望执行的内容出现在我启动 gui 的 shell 中,所有结果也都与我输入的命令完全一样(下面只是 'ls -l')。

#!/usr/bin/wish

wm title . "Console wishing thingy"
wm minsize . 400 400
# -------------------------------------------------------------

set buttonFrame   [labelframe .button_frame   -borderwidth 1 -text "Buttons to Click"]

set quit_button   [button $buttonFrame.quit_button -text "Quit" -command "exit"]
set run_it_button [button $buttonFrame.run_it_button -text "execute ls -l" -command {do_run}]
pack $buttonFrame.quit_button   -side left
pack $buttonFrame.run_it_button -side right

grid $buttonFrame   -sticky w -pady 5

proc do_run {} {
        exec ls -l
}

没有结果。没有错误。但是,也没有控制台输出。

I simply want what is executed to appear in the shell in which I launch the gui, and all results as well

视情况而定,您有多种选择:

(1) proc do_run {} { exec 1>@stdout 2>@stderr ls -la }

使用这些指令,Tcl 进程 运行 exec 将显示已执行命令的 stdout 和 stderr,就像它自己的一样。

(2) proc do_run {} { puts [exec ls -la] }

没有指令,如上所述,exec returns 是已执行命令的捕获标准输出。使用 puts 会将 returned 标准输出打印到 Tcl 进程的标准输出。 stderr 将导致 exec 到 return 一个 Tcl 错误,将以 wish,Tk 方式处理。

(3) proc do_run {} { exec >&@stdout ls -la }

如果您不想专门处理 stderr,这会将两者都模制到 Tcl 进程的标准输出中。

另见 https://wiki.tcl-lang.org/page/exec