This article will guide you to write a small bash script using zenity to create a calendar dialog. Of course, with a very short line, the zenity will completed this work. But I want to write more about it so that new beginners can understand it more easily and using bash create calendar dialog.
Use Zenity simple command
Table of Contents
When you use the --calendar
option, you will create a calendar dialog. Zenity returns the selected date to standard output. If no date is specified on the command line, the dialog uses the current date.
zenity --calendar
You can see in the below image, a small calendar dialog is display. The selected date is the current date (at the time I write this article – 02/01/2018)
Calendar dialog box supports options
The table below shows the options that the calendar dialog box supports. Note that these options will begin with two dashes at the beginning, because you may not see clearly in the table below:
Options | Meaning |
--text=text | Specifies the text that is displayed in the calendar dialog. |
--day=day | Specifies the day that is selected in the calendar dialog. day must be a number between 1 and 31 inclusive. |
--month=month | Specifies the month that is selected in the calendar dialog. month must be a number between 1 and 12 inclusive. |
--year=year | Specifies the year that is selected in the calendar dialog. |
--date-format=format | Specifies the format that is returned from the calendar dialog after date selection. The default format depends on your locale. Format must be a format that is acceptable to the strftime function, for example %A %d/%m/%y. |
For the --date-format
option in the table above, you can see this link to learn more about the values contained in the strftime function.
Bash create calendar dialog
Our aim is to use the Bash combined with Zenity to create a calendar dialog.
The content of the script below:
#!/bin/bash
# Script author: Danie Pham
# Script site: https://devopslite.com
# Script date: 02-01-2018
# Script use to create a calendar dialog by using Zenity
# Adding a habit of writing into functions will be useful when you write large programs.
f_create_calendar () {
if zenity --calendar \
--title="Demo create calendar dialog - WriteBash.com" \
--text="Click on a date to select that date." \
--day=1 --month=1 --year=2018
then echo $?
else
echo "No date selected"
fi
}
# Main function
f_main () {
f_create_calendar
}
f_main
exit
Copy the script above, assign permissions to script and execute script. The result will be as shown below. If you compare it to the image above the article, you will notice a difference.
chmod +x create_calendar
./create_calendar
Conclusion
Indeed, the option to use the command zenity –calendar not difficult, it can be said is very very easy. However, I want to write an article that the reader can understand it most clearly.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).