在shell脚本中,发现case语句的一个问题。
就是指定小写字母[a-z]和大写字母[A-Z]的这种方法不管用了。

出现如下情况:

复制代码 代码如下:
[root@station1 ~]# cat case.sh
#!/bin/bash
while :
do
echo -n "input a letter: "
read var
case "$var" in
  [a-z]) echo "Lowercase letter";;
  [A-Z]) echo "Uppercase letter";;
 [0-9]) echo "Digit";;
  *) echo "Punctuation, whitespace, or other";;
esac
done
[root@station1 ~]# bash case.sh
input a letter: a
Lowercase letter
input a letter: A
Lowercase letter
input a letter: 2
Digit
input a letter: 0
Digit
input a letter: B
Lowercase letter
input a letter: y
Lowercase letter
input a letter: ^C
[root@station1 ~]#

可以看到当输入大小写字母都会输出“Lowercase letter”

就当我疑惑不解的时候,奇迹发生了。。。。

复制代码 代码如下:
[root@station1 ~]# bash case.sh
input a letter: Z
Uppercase letter
input a letter:

当输入大写Z的时候,终于出现了我们想要的结果:Uppercase letter
后来在man bash文档中也没有关于"-"代表范围的说明,值说想匹配"-",就把"-"放到[]中最前面或者最后面。
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname
expansion (see Pathname Expansion below). The word is expanded using tilde expansion, parameter and variable expansion, arithmetic sub-
stitution, command substitution, process substitution and quote removal. Each pattern examined is expanded using tilde expansion, param-
eter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell option nocasematch is
enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the corresponding list is
executed. If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ;& in place of ;; causes
execution to continue with the list associated with the next set of patterns. Using ;;& in place of ;; causes the shell to test the next
pattern list in the statement, if any, and execute any associated list on a successful match. The exit status is zero if no pattern
matches. Otherwise, it is the exit status of the last command executed in list.

再看下面这段代码:

复制代码 代码如下:
[root@station1 ~]# cat case.sh
#!/bin/bash
while :
do
echo -n "input a letter: "
read var
case "$var" in
[a-c]) echo "Lowercase letter";;
[A-Z]) echo "Uppercase letter";;
[0-9]) echo "Digit";;
*) echo "Punctuation, whitespace, or other";;
esac
done
[root@station1 ~]# bash case.sh
input a letter: a
Lowercase letter
input a letter: b
Lowercase letter
input a letter: c
Lowercase letter
input a letter: d
Uppercase letter
input a letter: e
Uppercase letter
input a letter: ^C
[root@station1 ~]#

可以看出来它的编码方式是:aAbBcCdDeE...yYzZ
所以才会出现这种情况。这也算是一个小bug吧,如果想真的想达到我们想要的结果,可以用posix的[:upper:]。
个人想法:有时候出现这种情况也不是个坏事,或许还可以利用这个bug去做点事。

标签:
case,条件控制,bug

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。