Bash create calendar dialog with Zenity

by Daniel Pham
Published: Updated:
This entry is part 2 of 13 in the series GUI for bash script using Zenity

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

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)

Bash create calendar dialog with Zenity
Zenity calendar

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:

OptionsMeaning
--text=textSpecifies the text that is displayed in the calendar dialog.
--day=daySpecifies the day that is selected in the calendar dialog. day must be a number between 1 and 31 inclusive.
--month=monthSpecifies the month that is selected in the calendar dialog. month must be a number between 1 and 12 inclusive.
--year=yearSpecifies the year that is selected in the calendar dialog.
--date-format=formatSpecifies 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
Bash create calendar dialog with Zenity
Script to 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).

0 0 votes
Article Rating
Series Navigation«« Previous part: Introduction Zenity simple GUI for BashNext part: Zenity create a notification icon »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.