How to show a GUI message box from a bash script in linux?

Viewed 199696

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.

14 Answers

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.

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.

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

if nothing else is present. you can launch an xterm and echo in it, like this:

 xterm -e bash -c 'echo "this is the message";echo;echo -n "press enter to continue "; stty sane -echo;answer=$( while ! head -c 1;do true ;done);'

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"`

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

Zenity is really the exact tool that I think that you are looking for.

or

zenity --help

You can use shellmarks to display a GUI dialog prior to your shell script running, that will allow the user to enter data that will be placed in the environment.

#!/bin/bash
echo "Hello ${name}"
exit 0
---
[name]
  type="text"
  label="Please enter your name"
  required=true

Running script:

shellmarks hello.sh

Shellmarks dialog

If you enter "Steve" in the box and press run, the output will be

Hello Steve

Disclosure: I'm the author of Shellmarks

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

I'm liking what I'm seeing with script-dialog. It ticks all my boxes, plus some:

  • pop up GUI boxes, but has text-mode fallback
  • support for various sudo variants (gksudo, kde-sudo, ...)
  • can re-launch itself in terminal window

Indeed it's a wrapper for kdialog, zenity, dialog, whiptail and a custom fall-back.

Draw-back is that it doesn't have a CLI, but instead is meant to be sources into a bash script.

Related