Main index

Introducing UNIX and Linux


More on shells

Overview
Simple arithmetic
      Arithmetic expansion
            Operators for arithmetic expansion
      The 'expr' command
Pattern matching
      Patterns
            Examples of patterns
      The case statement
Entering and leaving the shell
More about scripts with options
Symbolic links
Setting up terminals
Conventions used in UNIX file systems
Summary
Exercises

Answer to chapter 8 question 5

Use date to display the hour, then pattern match on the output:

# Format %H gives the hour as 2 digits, 00-23
case $( date "+%H" ) in

   # Any hour 00 to 09, also 10 or 11
   0?|1[01]) echo Good Morning ;;

   # Any hour 12 to 17
   1[2-7])   echo Good afternoon ;;

   # Any other time is evening
   *)        echo Good evening ;;
esac

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck