DHD
DHD

Reputation: 27

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

I'm using a tcl gui only to assist in creating valid command lines which are otherwise too irritating to type. I simply want what is executed to appear in the shell in which I launch the gui, and all results as well, exactly as though I typed in the command (which below, is only '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
}

No results. No errors. But, no console output either.

Upvotes: 0

Views: 230

Answers (1)

mrcalvin
mrcalvin

Reputation: 3434

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

It depends, you have several options:

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

With these directives, the Tcl process running exec will show the executed command's stdout and stderr as it were its own.

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

Without directives, as above, exec returns the captured stdout of the executed command. Using puts will print the returned stdout to the Tcl process' stdout. stderr will cause exec to return a Tcl error, which will be handled in the wish, Tk way.

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

If you don't want to handle stderr specifically, this will mold both into the Tcl process's stdout.

See also https://wiki.tcl-lang.org/page/exec.

Upvotes: 1

Related Questions