Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499679
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类:

2012-09-17 11:13:42

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

  1. [root@station1 ~]# cat case.sh
  2. #!/bin/bash
  3. while :
  4. do
  5.     echo -n "input a letter: "
  6.     read var
  7.     case "$var" in
  8.       [a-z]) echo "Lowercase letter";;
  9.       [A-Z]) echo "Uppercase letter";;
  10.      [0-9]) echo "Digit";;
  11.       *) echo "Punctuation, whitespace, or other";;
  12. esac
  13. done
  14. [root@station1 ~]# bash case.sh
  15. input a letter: a
  16. Lowercase letter
  17. input a letter: A
  18. Lowercase letter
  19. input a letter: 2
  20. Digit
  21. input a letter: 0
  22. Digit
  23. input a letter: B
  24. Lowercase letter
  25. input a letter: y
  26. Lowercase letter
  27. input a letter: ^C
  28. [root@station1 ~]#

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

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

  1. [root@station1 ~]# bash case.sh
  2. input a letter: Z
  3. Uppercase letter
  4. input a letter:

当输入大写Z的时候,终于出现了我们想要的结果:Uppercase letter
后来在man bash文档中也没有关于"-"代表范围的说明,值说想匹配"-",就把"-"放到[]中最前面或者最后面。


  1. case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
  2. A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname
  3. expansion (see Pathname Expansion below). The word is expanded using tilde expansion, parameter and variable expansion, arithmetic sub-
  4. stitution, command substitution, process substitution and quote removal. Each pattern examined is expanded using tilde expansion, param-
  5. eter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell option nocasematch is
  6. enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the corresponding list is
  7. executed. If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ;& in place of ;; causes
  8. execution to continue with the list associated with the next set of patterns. Using ;;& in place of ;; causes the shell to test the next
  9. pattern list in the statement, if any, and execute any associated list on a successful match. The exit status is zero if no pattern
  10. matches. Otherwise, it is the exit status of the last command executed in list.

看下下面这个代码:

点击(此处)折叠或打开

  1. [root@station1 ~]# cat case.sh
  2. #!/bin/bash
  3. while :
  4. do
  5. echo -n "input a letter: "
  6. read var
  7. case "$var" in
  8. [a-c]) echo "Lowercase letter";;
  9. [A-Z]) echo "Uppercase letter";;
  10. [0-9]) echo "Digit";;
  11. *) echo "Punctuation, whitespace, or other";;
  12. esac
  13. done
  14. [root@station1 ~]# bash case.sh
  15. input a letter: a
  16. Lowercase letter
  17. input a letter: b
  18. Lowercase letter
  19. input a letter: c
  20. Lowercase letter
  21. input a letter: d
  22. Uppercase letter
  23. input a letter: e
  24. Uppercase letter
  25. input a letter: ^C
  26. [root@station1 ~]#

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

阅读(271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~