This shell script dynamically generates greetings based on the current hour using the 'date' command and a 'case' statement. Here's a breakdown:

hour=`date +%H`
case $hour in
  08|09|10|11|12) echo 'Good Morning!' ;;
  13|14|15|16|17) echo 'Good Afternoon!' ;;
  18|19|20|21|22) echo 'Good Evening!' ;;
          *) echo 'Hello! ' ;;
esac

Explanation:

  • hour=date +%H``: Retrieves the current hour from the system and assigns it to the variable 'hour'.
  • case $hour in: Initiates a 'case' statement to evaluate the value of 'hour'.
  • 08|09|10|11|12) echo 'Good Morning!' ;;: Outputs 'Good Morning!' if 'hour' is between 8 and 12 (inclusive).
  • 13|14|15|16|17) echo 'Good Afternoon!' ;;: Outputs 'Good Afternoon!' if 'hour' is between 13 and 17 (inclusive).
  • 18|19|20|21|22) echo 'Good Evening!' ;;: Outputs 'Good Evening!' if 'hour' is between 18 and 22 (inclusive).
  • *) echo 'Hello! ' ;;: Outputs 'Hello!' if 'hour' doesn't fall into any of the previous ranges.
  • esac: Ends the 'case' statement.

This script demonstrates how to use conditional logic in shell scripting to create dynamic and time-sensitive outputs. It provides a simple example of utilizing system information to personalize user interaction.

Shell Script: Dynamic Greetings Based on Time of Day

原文地址: https://www.cveoy.top/t/topic/jjPb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录