Nandakumar Edamana
Share on:
@ R t f
Section: shell-script

Display a Time-dependent Greeting Message Using Shell Script


The following shell script displays "Good morning!", "Good afternoon!" or "Good evening!" based on the time it gets executed. It stores the output of date +%H in the variable hour, and this is taken as the basis for decision making.

The +%H option tells the date command that it is 24-hour format hour that is to be printed (or redirected to our script).

#!/bin/sh

hour="$(date +%H)" # Hour of the day

if [ "$hour" -lt 12 ]; then
	echo "Good morning!"
elif [ "$hour" -lt 16 ]; then
	echo "Good afternoon!"
else
	echo "Good evening!"
fi

Click here to read more like this. Click here to send a comment or query.