- let's create a directory and some files for this example
$ cd
$ mkdir caserdir
$ cd casedir
$ touch hello.text hello2.text
- 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
- run this script three times and each time use a different argument a, b and then c.
- 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
- now let's create some files in this directory
$ touch a b c d e f
$ touch aa ab ac bb cc
- re-run the for loop on the command line again
- 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
- re-run the for loop on the command line again
- 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
- re-run the for loop on the command line again
- 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
- re-run the for loop on the command line again
- did you get what you expected??? how would you fix this?