Bash 大/小写检查 case 语句

问题描述 投票:0回答:1

我使用 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
bash case-statement
1个回答
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] $

请参阅 Bash 手册中的 shopt -s extglob模式匹配

© www.soinside.com 2019 - 2024. All rights reserved.