Guest User

Untitled

a guest
Jan 10th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.29 KB | None | 0 0
  1. Lab Solution: Using the case Statement
  2. Create a file named testcase.sh, with the content below.
  3. #!/bin/bash
  4.  
  5. # Accept a number between 1 and 12 as
  6. # an argument to this script, then return the
  7. # the name of the month that corresponds to that number.
  8.  
  9. # Check to see if the user passed a parameter.
  10. if [ $# -eq 0 ]
  11. then
  12.   echo "Error. Give as an argument a number between 1 and 12."
  13.   exit 1
  14. fi
  15.  
  16. # set month equal to argument passed for use in the script
  17. month=$1
  18.  
  19. ################################################
  20. # The example of a case statement:
  21.  
  22. case $month in
  23.  
  24.   1)  echo "January"   ;;
  25.   2)  echo "February"  ;;
  26.   3)  echo "March"     ;;
  27.   4)  echo "April"     ;;
  28.   5)  echo "May"       ;;
  29.   6)  echo "June"      ;;
  30.   7)  echo "July"      ;;
  31.   8)  echo "August"    ;;
  32.   9)  echo "September" ;;
  33.   10) echo "October"   ;;
  34.   11) echo "November"  ;;
  35.   12) echo "December"  ;;
  36.   *)
  37.      echo "Error. No month matches: $month"
  38.      echo "Please pass a number between 1 and 12."
  39.      exit 2
  40.      ;;
  41. esac
  42. exit 0
  43. Make it executable and run it:
  44. student:/tmp> chmod +x testcase.sh
  45. student:/tmp> ./testcase.sh 5
  46. May
  47. student:/tmp> ./testcase.sh 12
  48.  
  49. December
  50.  
  51. student:/tmp> ./testcase.sh 99
  52.  
  53. Error. No month matches: 99
  54. Please pass a number between 1 and 12
  55. student:/tmp>
Advertisement
Add Comment
Please, Sign In to add comment