Shell script to display Good Morning, Good Afternoon Good Evening according to system time
In this article, we will see how to print Good Morning, Good Afternoon, Good Evening, and Good Night, according to system time using shell programming
Example :
# Program to print Good Morning, # Good Afternoon, Good Evening $ and Good Night, according to $ system time. #!/bin/bash hour=`date +%H` if [ $hour -lt 12 ] # if hour is less than 12 then echo "GOOD MORNING WORLD" elif [ $hour -le 16 ] # if hour is less than equal to 16 then echo "GOOD AFTERNOON WORLD" elif [ $hour -le 20 ] # if hour is less than equal to 20 then echo "GOOD EVENING WORLD" else echo "GOOD NIGHT WORLD" fi
The above shell script displays “Good morning!”, “Good afternoon!”, “Good evening” or “Good night” based on the time it gets executed in the system. The program will have .sh extension.
The variable “hour” will hold the value of `date +%H`. The command “date +%H” returns the hour in 24-hour format. The command “echo” will simply print the statement if it satisfies the condition.
Output :
Example 2:
#!/bin/bash hour=$(date +%H) if [ $hour -lt 12 ] then greet="Good Morning" elif [ $hour -le 16 ] then greet="Good Afternoon" elif [ $hour -lt 20 ] then greet="Good Evening" else greet="Good Night" fi echo "$greet"
In this example, we have declared a new variable ‘greet’, which is holding “Good Morning”, “Good Afternoon”, “Good Evening” and “Good Night”. Depending on the time of the system, it will print Good morning, Good afternoon, Good Evening, or Good Night to the user.