Search notes:

Shell command: zenity

zenity can be used to create message boxes in a shell script.

Infos, warnings, errors etc

In its simplest form, zenity can be used to create a message box with a info, warning or error.
Note: with --notification option, the flow of the script is not stopped.
#/bin/bash

zenity --notification --text "This is a notification.\nNote: the shell script does not stop here!"
zenity --info         --text "This is a info!"
zenity --warning      --text "This is a warning!"
zenity --error        --text "This is a error!"
Github repository shell-commands, path: /zenity/info-warning-error-etc.sh

Entering values

#/bin/bash

value=$(zenity --entry --title "Entry of a value" --text "Enter a value")
echo you have entered $value;
Github repository shell-commands, path: /zenity/enter-value.sh
For sensitive data, such as passwords, the --hide-text option can be used in which case the entered data is not shown:
#/bin/bash

password=$(zenity --entry --hide-text --title "Entry of a password" --text "Enter your secret")
echo The secret password is $password
Github repository shell-commands, path: /zenity/enter-password.sh
#/bin/bash

# Note --step does not seem to have any influence
value=$(zenity --scale --text "Choose value between 10 and 42" --min-value 10 --max-value 42 --value 20 --step 2)
echo value is $value
Github repository shell-commands, path: /zenity/scale.sh

Yes no questions

Questions that can be answered with either yes or no can be asked with --question.
#/bin/bash

zenity --question  --text "This is a question!"
if [[ $? = 0 ]]; then
  echo "You answered yes";
else
  echo "You answered no";
fi

Github repository shell-commands, path: /zenity/question.sh
It's possible to add a timeout. If the time out is reached, the exit status $? is set to 5.
#/bin/bash

zenity --question  --text "This is a question!\nYou have five seconds to answer." --timeout 5
case $? in
  0) echo "You answered yes";;
  1) echo "You answered no" ;;
  5) echo "Time out"        ;;
  *) echo "Unexpected!"     ;;
esac
Github repository shell-commands, path: /zenity/question-timeout.sh

Choosing

Use the file selector to choose a file:
#/bin/bash

date=$(zenity --calendar --text "Choose your date")
echo "You have chosen $date" # Format is dd.mm.yyyy
Github repository shell-commands, path: /zenity/choose-date.sh
Use the calendar to choose a date:
#/bin/bash

file=$(zenity --file-selection --text "Choose your file")
echo "You have chosen $file"
Github repository shell-commands, path: /zenity/select-file.sh

Progress bar

#/bin/bash

percent=0;
for num in zero one two three four five six seven eight nine ten; do
  echo $percent
  echo "# $num"
  percent=$(($percent + 10));
  sleep 1
done | zenity --progress --text "Counting..." --percentage=0
Github repository shell-commands, path: /zenity/progress-bar.sh

See also

wmctrl, dialog
Shell commands

Index