case statement

  1. let's create a directory and some files for this example

    $ cd
    $ mkdir caserdir
    $ cd  casedir
    $ touch hello.text hello2.text
    

  2. Here are the contents of the script called case.sh:

    #!/usr/bin/sh
    
    x=$1
    echo "argument was $1"
    case $x in
    
    a) echo "got a" ;;
    b) echo "got b" ;;
    *) echo "what ??" ;;
    
    esac
    
    

  3. run this script three times and each time use a different argument a, b and then c.

  4. now let's write a for loop on the command line and give it different arguments

    $ for x in *
    > do
    > sh case.sh $x
    > done
    

  5. now let's create some files in this directory

    $ touch a b c d e f
    $ touch aa ab ac bb cc
    

  6. re-run the for loop on the command line again
  7. edit the script by adding one line to the case statement:

    #!/usr/bin/sh
    
    x=$1
    echo "argument was $1"
    case $x in
    
    a) echo "got a" ;;
    b) echo "got b" ;;
    ??) echo " got two characters" ;;
    *) echo "what ??" ;;
    
    esac
    

  8. re-run the for loop on the command line again
  9. edit the script by adding one line to the case statement:

    #!/usr/bin/sh
    
    x=$1
    echo "argument was $1"
    case $x in
    
    a) echo "got a" ;;
    b) echo "got b" ;;
    a?) echo " got two characters starts with a" ;;
    ??) echo " got two characters" ;;
    *) echo "what ??" ;;
    
    esac
    

  10. re-run the for loop on the command line again
  11. edit the script by adding one line to the case statement:

    #!/usr/bin/sh
    
    x=$1
    echo "argument was $1"
    case $x in
    
    a) echo "got a" ;;
    b) echo "got b" ;;
    a?) echo " got two characters starts with a" ;;
    ??) echo " got two characters" ;;
    b?) echo " got two characters starts with b" ;;
    *) echo "what ??" ;;
    
    esac
    

  12. re-run the for loop on the command line again
  13. did you get what you expected??? how would you fix this?