Working with calendars in the shell
Tutorial – Shell Calendars
Command-line aficionados do not have to forgo calendars and appointment reminders. The shell offers many tools for user-friendly handling of date definitions in scripts.
Working with dates in the shell often causes problems. Aside from the usual typos, this is usually due to mismatched language and country settings. In practice, you will especially be confronted with this when working with databases.
Manual input can be relatively easily checked for formal correctness. The simplest aspect to change is the separators or the order of day, month, and year. An example of this is shown in Listing 1, and the corresponding output is shown in Figure 1. The short script supports both correct input with the dot as a separator and input with the comma in the numeric keypad for quick data entry.
Listing 1
Formatting the Date
#!/bin/bash # Comma as field separator # Output in format YYYY-MM-DD # Input read -p "Enter date: " input # Replace comma with dot for uniformity input=$(echo $input | tr \, \. ) # Output to variable with Awk, minus sign as separator output=$(echo $input | awk -F \. '{ print $3 "-" $2 "-" $1 } ') echo "Date converted: $output"
dialog and YAD
If you attach importance to user-friendliness as well as correct date values, why not use the attractive (semi-)graphical input options offered by dialog
or YAD?
With dialog
[3], simple date input takes the basic form of:
dialog --calendar 0 0
The two zeros automatically set the size to match the terminal. In the calling shell script, dialog
's various exit codes can be used for control, as shown in Listing 2. In the resulting mask (Figure 2), you can use the tab and arrow keys to navigate and press the Enter key to complete the entry.
Listing 2
A dialog Query
#!/bin/bash date=$(dialog --stdout \ --title "Calendar" \ --calendar "Select date" 0 0) if [ $? -eq 0 ]; then datum=$(echo $date | tr \/ \.) echo "Input date: $date" elif [ $? -eq 1 ]; then echo "Input canceled" fi
YAD [4] lets you output a shell script's user dialog in the GUI. Because of the very extensive options, you might want to read the YAD's documentation or at least excerpts of it. The input is conveniently mouse controlled. In the example shown in Listing 3, I replaced the standard buttons with my own, resulting in different exit codes (i.e., 2
for input). You can see the output of the script in Figure 3.
Listing 3
A YAD Query
#!/bin/bash date=$(yad --title=Calendar \ --text="Select date" \ --calendar --show-weeks \ --button="Cancel":1 \ --button="Intput":2) if [ $? -eq 2 ]; then date=$(echo $date | tr \/ \.) echo "Input date: $date" elif [ $? -eq 1 ]; then echo "Cancel entry" fi
smenu
Display redirection via SSH does not work everywhere. In addition, displaying dialog
masks can cause problems with some terminal settings. However, there is another method you can use if it is important to avoid incorrect entries (such as April 31). You specify the possible values with smenu [5].
For the calendar year, the user can select the current, previous, and next year in Listing 4 (line 7). The selection for the day works in the same way (line 17). To save space in the terminal display, smenu's -n1
option is used to limit the selection to one line. You can use the arrow keys to select the day. A cal
command (line 16) determines beforehand which specifications are permissible for it. The script extracts additional specifications such as the month, year, and weekday from this. To make the date suitable for further processing, single-digit day specifications are given a leading zero (lines 19 to 20). (Figure 4 shows the script output from Listing 4.)
Listing 4
Querying a Date with smenu
01 #!/bin/bash 02 # Year: current, last, next 03 ayear=$(date +%Y) 04 vyear=$(echo $ayear -1 | bc) 05 fyear=$(echo $ayear + 1 | bc) 06 # Use Smenu to select months 07 year=$(echo $vyear $ayear $fyear | smenu -m "Select calendar year") 08 echo "" 09 echo "------------------------" 10 echo "" 11 monat=$(echo 01 02 03 04 05 06 07 08 09 10 11 12 | smenu -m "Select month" ) 12 echo "------------------------" 13 # Create header for selection 14 menuhead=$(cal $month $year | head -1) 15 # Calendar field 16 calendar=$(cal $month $year | tail -6 | tr -d [:alpha:]) 17 day=$(echo $calendar | smenu -m "Use arrow keys to select day" -t1 -n1 ) 18 # Prepend zero to single-figure days 19 if [ $(echo $day | wc -L) -eq 1 ]; then 20 day=$(echo "0$day") 21 fi 22 date=$(echo $day.$month.$year) 23 echo $date
Shell Calendars
To display a calendar, you can use cal
or the spruced-up, newer ncal
variant, which the popular distributions already preinstall. The options for both programs can be found in Table 1. Called with the -C
parameter, ncal
behaves exactly like cal
. Figure 5 shows the two resulting calendars, which display three months along with the week numbers.
Table 1
cal and ncal Options
Option |
ncal |
cal |
Do not highlight today |
|
– |
Month (current year) |
|
– |
Month (any year) |
|
|
Annual calendar for year |
|
|
Calendar this year |
|
|
Three-month calendar |
|
|
Last month |
|
– |
Next month |
|
– |
Number weeks |
|
|
Display weeks horizontally |
|
– |
Display weeks vertically |
– |
|
1 |
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.