KSH 脚本检查 case 语句中的字母数字
KSH script checks for alphanumeric in case statement
下面是我要实现的简化模型:
#!bin/ksh
string=AUS00
case $string in
[[:alnum:]] ) echo "alphanumeric" ;;
*) echo "nope" ;;
esac
我无法验证字母数字代码。
约束条件:
- 验证需要在 case 语句中进行
- 不支持 alnum 函数
- 仅阳性检查。无法检查是否缺少字母数字。
非常感谢
模式 [[:alnum:]]
将匹配 单个字母数字字符 。您的字符串超过一个字符,因此不匹配。
如果你想检查你的字符串 包含 一个字母字符,你需要 *[[:alnum:]]*
如果你想检查你的字符串 只包含 个 alnum 字符,我会翻转检查以查看字符串是否包含非 alnum 字符:
for string in alnumOnly 'not all alnum'; do
case "$string" in
*[^[:alnum:]]*) echo "$string -> nope" ;;
*) echo "$string -> alphanumeric" ;;
esac
done
alnumOnly -> alphanumeric
not all alnum -> nope
我意识到 ksh(甚至 ksh88)实现了 bash 所描述的 "extended patterns":
A pattern-list is a list of one or more patterns separated
from each other with a |. Composite patterns can be formed
with one or more of the following:
?(pattern-list)
Optionally matches any one of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given
patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
@(pattern-list)
Matches exactly one of the given patterns.
!(pattern-list)
Matches anything, except one of the given patterns.
所以我们可以这样做:
case "$string" in
+([[:alnum:]]) ) echo "$string -> alphanumeric" ;;
* ) echo "string -> nope" ;;
esac
下面是我要实现的简化模型:
#!bin/ksh
string=AUS00
case $string in
[[:alnum:]] ) echo "alphanumeric" ;;
*) echo "nope" ;;
esac
我无法验证字母数字代码。
约束条件:
- 验证需要在 case 语句中进行
- 不支持 alnum 函数
- 仅阳性检查。无法检查是否缺少字母数字。
非常感谢
模式 [[:alnum:]]
将匹配 单个字母数字字符 。您的字符串超过一个字符,因此不匹配。
如果你想检查你的字符串 包含 一个字母字符,你需要 *[[:alnum:]]*
如果你想检查你的字符串 只包含 个 alnum 字符,我会翻转检查以查看字符串是否包含非 alnum 字符:
for string in alnumOnly 'not all alnum'; do
case "$string" in
*[^[:alnum:]]*) echo "$string -> nope" ;;
*) echo "$string -> alphanumeric" ;;
esac
done
alnumOnly -> alphanumeric
not all alnum -> nope
我意识到 ksh(甚至 ksh88)实现了 bash 所描述的 "extended patterns":
A pattern-list is a list of one or more patterns separated from each other with a |. Composite patterns can be formed with one or more of the following:
?(pattern-list) Optionally matches any one of the given patterns.
*(pattern-list) Matches zero or more occurrences of the given patterns.
+(pattern-list) Matches one or more occurrences of the given patterns.
@(pattern-list) Matches exactly one of the given patterns.
!(pattern-list) Matches anything, except one of the given patterns.
所以我们可以这样做:
case "$string" in
+([[:alnum:]]) ) echo "$string -> alphanumeric" ;;
* ) echo "string -> nope" ;;
esac