Thursday, July 28, 2011

How to create dialog boxes in shell scripting using zenity tool

zenity is a simple gui creation tool which comes default in ubuntu 11.04.you can make use of this tool to create nice dialog boxes in your shell scripts.

example explaination:

To ask a question from user add the following lines to where it should prompt.
zenity --question --text "Are you sure you want to continue ?"
echo $?

$? variable will contain the output of the GUI.

if user clicks yes $? variable will contain 0
if user clicks no $? variable will contain 1

according to the content of the $? you can do whatever you wish.

These are some other dialog boxes you can use.

1.To give warnings to the user

zenity --warning --text "This will kill, are you sure?";echo $?

2.To create a radio list

ans=$(zenity --list --text "How good are you in java?" --radiolist --column "Pick" --column "Opinion" TRUE expert FALSE Average FALSE "moderate" FALSE "newbe"); echo $ans

this will print the option user selected.

3. To create a check list

ans=$(zenity --list --text "What do you want?" --checklist --column "Pick" --column "options" TRUE "rice" TRUE "egg" FALSE "fish" FALSE "chicken" --separator=":"); echo $ans

this will print: rice:egg


4.To get a date from user

szDate=$(zenity --calendar --text "Pick a day" --title "Medical Leave" --day 30 --month 7 --year 2011); echo $szDate

this will print date as follows:
07/30/11


5.To get text input from user

szAnswer=$(zenity --entry --text "what is your name?" --entry-text ""); echo $szAnswer

this will print the user input.

6.To give the error message

zenity --error --text "Installation failed! "

7.To give information to the user

zenity --info --text "this is a blog you can get more geek stuff"


you can find out more zenity dialog boxes through internet.
Enjoy using zenity dialog boxes!!!!

1 comment:

  1. A great post sampath.Now we can have our own simple GUI s built using zenity.

    Can you please provide us the link for zenity manual?

    ReplyDelete