| 1 | #!/bin/bash |
| 2 | # Testing ranges of characters. |
| 3 | |
| 4 | echo; echo "Hit a key, then hit return." |
| 5 | read Keypress |
| 6 | |
| 7 | case "$Keypress" in |
| 8 | [[:lower:]] ) echo "Lowercase letter";; |
| 9 | [[:upper:]] ) echo "Uppercase letter";; |
| 10 | [0-9] ) echo "Digit";; |
| 11 | * ) echo "Punctuation, whitespace, or other";; |
| 12 | esac # Allows ranges of characters in [square brackets], |
| 13 | #+ or POSIX ranges in [[double square brackets. |
| 14 | |
| 15 | # In the first version of this example, |
| 16 | #+ the tests for lowercase and uppercase characters were |
| 17 | #+ [a-z] and [A-Z]. |
| 18 | # This no longer works in certain locales and/or Linux distros. |
| 19 | # POSIX is more portable. |
| 20 | # Thanks to Frank Wang for pointing this out. |
| 21 | |
| 22 | # Exercise: |
| 23 | # -------- |
| 24 | # As the script stands, it accepts a single keystroke, then terminates. |
| 25 | # Change the script so it accepts repeated input, |
| 26 | #+ reports on each keystroke, and terminates only when "X" is hit. |
| 27 | # Hint: enclose everything in a "while" loop. |
| 28 | |
| 29 | exit 0 |