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

LinuxBashScriptingUbuntuGtk

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

In many Linux distros the notify-send command will throw one of those nice perishable notifications in the top right corner. Like so:

notify-send "My name is bash and I rock da house"

B.e.a.utiful!

Solution 2 - Linux

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.

Solution 3 - Linux

Everyone mentions zenity, there seem to be many others. A mixed up but interesting list is at http://alternativeto.net/software/zenity/

First, an example of zenity featuring text formatting markup, window title, button label.

zenity \
--info \
--text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nGet your <b>coffee</b>." \
--title="Coffee time" \
--ok-label="Sip"

gxmessage

gxmessage "my text"

xmessage

xmessage is very old so it is stable and probably available in all distributions that use X (since it's distributed with X). It is customizable through X resources, for those that have been using Linux or Unix for long enough to know what it means (.Xdefaults, anyone ?).

xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse "Is xmessage enough for the job ?" -timeout 10

kdialog (KDE tool)

kdialog --error "Some error occurred"

In a PPA

YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog. Does not seem to auto-size dialogs.

echo My text | yad \
--text-info \
--width=400 \
--height=200

An bigger example

yad \
--title="Desktop entry editor" \
--text="Simple desktop entry editor" \
--form \
--field="Type:CB" \
--field="Name" \
--field="Generic name" \
--field="Comment" \
--field="Command:FL" \
--field="Icon" \
--field="In terminal:CHK" \
--field="Startup notify:CHK" "Application" "Name" "Generic name" "This is the comment" "/usr/bin/yad" "yad" FALSE TRUE \
--button="WebUpd8:2" \
--button="gtk-ok:0" \
--button="gtk-cancel:1"

Others not in Ubuntu standard repositories

  • shellgui
  • xdialog
  • gtkdialog

Off-topic (for terminal)

whiptail --msgbox "my text" 10 20
dialog --msgbox "my text" 10 20

Feel free to edit.

Solution 4 - Linux

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.

Solution 5 - Linux

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

Solution 6 - Linux

alert and notify-send seem to be the same thing. I use notify-send for non-input messages as it doesn't steal focus and I cannot find a way to stop zenity etc. from doing this.

e.g.

# This will display message and then disappear after a delay:
notify-send "job complete"

# This will display message and stay on-screen until clicked:
notify-send -u critical "job complete"

Solution 7 - Linux

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);'

Solution 8 - Linux

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

Solution 9 - Linux

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

Solution 10 - Linux

How about Ubuntu's alert. It can be used after any operation to alert it finished and even show red cross icon if operaton was finnished with errors

ls -la; alert

Solution 11 - Linux

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

or

zenity --help

Solution 12 - Linux

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

Solution 13 - Linux

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

Solution 14 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlorgbeardView Question on Stackoverflow
Solution 1 - LinuxchmacView Answer on Stackoverflow
Solution 2 - LinuxDerek ParkView Answer on Stackoverflow
Solution 3 - LinuxStéphane GourichonView Answer on Stackoverflow
Solution 4 - LinuxJimView Answer on Stackoverflow
Solution 5 - LinuxBlorgbeardView Answer on Stackoverflow
Solution 6 - LinuxDave ThebuskerukView Answer on Stackoverflow
Solution 7 - Linuxuser6795571View Answer on Stackoverflow
Solution 8 - LinuxMark HarrisonView Answer on Stackoverflow
Solution 9 - LinuxSteve BakerView Answer on Stackoverflow
Solution 10 - LinuxmulyaView Answer on Stackoverflow
Solution 11 - LinuxSandro BView Answer on Stackoverflow
Solution 12 - Linuxsteve hannahView Answer on Stackoverflow
Solution 13 - LinuxJarekView Answer on Stackoverflow
Solution 14 - LinuxBendlasView Answer on Stackoverflow