views:

5079

answers:

10

I'm writing a few little bash scripts under Ubuntu linux. I want to be able to run them from the GUI without needing a terminal window to enter any input or view any output.

So far the only input required is a password for sudo - and gksudo handles that fine. But I haven't found an easy way to show a message box yet. Is there some kind of 'gkmessage' command available? I'd prefer something present in a default Ubuntu install, but I don't mind installing a new package if necessary.

A: 

Ok so I found the xmessage command, which is sort of good enough.

If anyone knows of something better - ie. uses GTK widgets and so on, please post :)

Blorgbeard
+1  A: 

Here's a little Tcl script that will do what you want. The Wish interpreter should be installed by default on Ubuntu.

#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent

Call it like this:

myanswer=`gui-prompt "type your answer and press enter"`
Mark Harrison
+12  A: 

I believe Zenity will do what you want. It's specifically designed for displaying GTK dialogs from the command line, and it's available as an Ubuntu package.

Derek Park
A: 

Mark, thanks - but Tk was not installed by default on my Ubuntu (I should perhaps have mentioned that it's Ubuntu-Eee). However, Zenity actually was installed already - so I've accepted Derek's answer.

Blorgbeard
You can add *comments* to the answers -- please do not post your replies in seperate *answers*.
Török Gábor
This answer was posted before comments were implemented, newbie :P
Blorgbeard
+3  A: 

There is also dialog and the KDE version kdialog. Dialog is used by slackware, so it might not be immediately available on other distributions.

Steve Baker
A: 

Kdialog and dialog are both good, but I'd recommend Zenity. Quick, easy, and much better looking the xmessage or dialog.

Jarek
A: 

zentiy is really the exact tool that I think that you are looking for http://live.gnome.org/Zenity

or $zenity --help

A: 

Dialog is used by slackware, so it might not be immediately available on other distributions.

Huh? cdialog is a very common package, a quick check of the changelog in the Mandriva package shows it's been in that distro for over 4 years now.

It's not installed on a default Ubuntu 8.10 though.
Blorgbeard
A: 

i would like to suggest "dzen".. thanks

+2  A: 

The zenity application appears to be what you are looking for.

To take input from zenity, you can specify a variable and have the output of zenity --entry saved to it. It looks something like this:

my_variable=$(zenity --entry)

If you look at the value in my_variable now, it will be whatever was typed in the zenity pop up entry dialog.

If you want to give some sort of prompt as to what the user (or you) should enter in the dialog, add the --text switch with the label that you want. It looks something like this:

my_variable=$(zenity --entry --text="What's my variable:")

Zenity has lot of other nice options that are for specific tasks, so you might want to check those out as well with zenity --help. One example is the --calendar option that let's you select a date from a graphical calendar.

my_date=$(zenity --calendar)

Which gives a nicely formatted date based on what the user clicked on:

echo ${my_date}

gives:

08/05/2009

There are also options for slider selectors, errors, lists and so on.

Hope this helps.

Jim