Bash大写/小写检查到case语句

时间:2015-02-07 15:22:26

标签: bash case-statement

我有使用if条件写的大写或小写字符检查,如何用case语句?

char=""
echo "Type char:"
read char

if [ $char = ${char^} ]; then
echo "upper case"
elif [ $char = ${char,,} ]; then
echo "lower case"
else
echo "neither"
fi

1 个答案:

答案 0 :(得分:2)

例如:

[STEP 101] $ cat foo.sh
shopt -s extglob
read -p "Type char: " ch
case $ch in
    +([[:lower:]])) echo lowercase;;
    +([[:upper:]])) echo uppercase;;
    *)              echo neither;;
esac
[STEP 102] $ ./foo.sh
Type char: abc
lowercase
[STEP 103] $ ./foo.sh
Type char: ABC
uppercase
[STEP 104] $ ./foo.sh
Type char: aBc
neither
[STEP 105] $

请参阅shopt -s extglob中的pattern matchingBash's manual