OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Patterns

The patterns used by case are the same as those used by pathname expansion. Patterns are terminated with a “)” character. Here are some valid patterns:


Table 31- 1: case Pattern Examples


Pattern Description

Pattern Description

a) Matches if word equals “a”.


image

[[:alpha:]]) Matches if word is a single alphabetic character.


image

???) Matches if word is exactly three characters long.


image

*.txt) Matches if word ends with the characters “.txt”.


image

*) Matches any value of word. It is good practice to include this as the last pattern in a case command, to catch any values of word that did not match a previous pattern; that is, to catch any possible invalid values.


image


Here is an example of patterns at work:


#!/bin/bash

read -p "enter word > " case $REPLY in

[[:alpha:]]) echo "is a single alphabetic character." ;; [ABC][0-9]) echo "is A, B, or C followed by a digit." ;;

???) echo "is three characters long." ;;

*.txt) echo "is a word ending in '.txt'" ;;

*) echo "is something else." ;; esac

#!/bin/bash

read -p "enter word > " case $REPLY in

[[:alpha:]]) echo "is a single alphabetic character." ;; [ABC][0-9]) echo "is A, B, or C followed by a digit." ;;

???) echo "is three characters long." ;;

*.txt) echo "is a word ending in '.txt'" ;;

*) echo "is something else." ;; esac


It is also possible to combine multiple patterns using the vertical bar character as a sepa- rator. This creates an “or” conditional pattern. This is useful for such things as handling both upper- and lowercase characters. For example:



#!/bin/bash

# case-menu: a menu driven system information program clear

echo "

Please Select:


A. Display System Information

B. Display Disk Space

C. Display Home Space Utilization

Q. Quit "

read -p "Enter selection [A, B, C or Q] > "


case $REPLY in

q|Q) echo "Program terminated." exit

;;

a|A) echo "Hostname: $HOSTNAME" uptime

;;

b|B) df -h

;;

c|C) if [[ $(id -u) -eq 0 ]]; then

echo "Home Space Utilization (All Users)" du -sh /home/*

else

echo "Home Space Utilization ($USER)" du -sh $HOME

fi

#!/bin/bash

# case-menu: a menu driven system information program clear

echo "

Please Select:


A. Display System Information

B. Display Disk Space

C. Display Home Space Utilization

Q. Quit "

read -p "Enter selection [A, B, C or Q] > "


case $REPLY in

q|Q) echo "Program terminated." exit

;;

a|A) echo "Hostname: $HOSTNAME" uptime

;;

b|B) df -h

;;

c|C) if [[ $(id -u) -eq 0 ]]; then

echo "Home Space Utilization (All Users)" du -sh /home/*

else

echo "Home Space Utilization ($USER)" du -sh $HOME

fi


;;

*) echo "Invalid entry" >&2 exit 1

;;

esac

;;

*) echo "Invalid entry" >&2 exit 1

;;

esac


Here, we modify the case-menu program to use letters instead of digits for menu selec- tion. Notice how the new patterns allow for entry of both upper- and lowercase letters.


Top OS Cloud Computing at OnWorks: