Abhishake Yadav
Abhishake Yadav

Reputation: 107

run shell script from tk GUI

I have have 5 different shell scripts which do data extraction from different log files, I have written a GUI in Tcl/Tk to consolidate all the scripts to an interface and have also made input entry widgets to provide folder locations to variables used in shell scripts where logs are stored , one thing that I didn't get a hold of is how to integrate my shell scripts with Tk GUI command buttons and display output in text widgets.

Any suggestions.

Thanks

Upvotes: 0

Views: 963

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

If the shell scripts are fairly quick to run, the best method is to use exec.

# If the scripts are marked executable
set output [exec thescript.sh $argumentOne $argumentTwo]
# If the scripts are not marked executable (assuming that they're really bash scripts)
set output [exec bash thescript.sh $argumentOne $argumentTwo]

You can set up the arguments by binding an entry or ttk::entry widget to a global variable and using that, or you can use any number of other mechanisms. (There's a vast number of possibilities.) The output is placed in the output variable; you can display it by inserting it into a text widget:

set outputwidget [text .t]
# Remember to put this widget in the right place in your GUI!
#    pack $outputwidget

# Put the output in the widget
$outputwidget insert end $output

# Stop the user from editing the contents.
# Note that you need to change the state to normal from code to update it from your script
$outputwidget configure -state readonly

There are a lot more things you can do. There are additional options to exec that can help in some circumstances, there's whole possibilities for processing the text before showing it to the user, and there's many things you can do with tagging the text in the widget to do things like syntax highlighting, etc. Many of these things are complex enough that you'll need to as separate questions on them.

Also, if you have long-running scripts then you'll either be using a pipeline (created with open |…) or the (old but excellent) Expect package. These are also big topics in themselves.

Upvotes: 1

Related Questions