shell脚本中case条件控制语句的一个bug分析

所属分类: 脚本专栏 / linux shell 阅读数: 2005
收藏 0 赞 0 分享

在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去做点事。

更多精彩内容其他人还在看

shell结合expect写的批量scp脚本工具

expect用于自动化地执行linux环境下的命令行交互任务,例如scp、ssh之类需要用户手动输入密码然后确认的任务。有了这个工具,定义在scp过程中可能遇到的情况,然后编写相应的处理语句,就可以自动地完成scp操作了
收藏 0 赞 0 分享

备份shell脚本实例代码

备份shell脚本一例,有需要的朋友可以参考下
收藏 0 赞 0 分享

shell中冒号的特殊用法分享

有关shell中冒号的特殊用法,供朋友们参考
收藏 0 赞 0 分享

Shell脚本学习指南之文本处理工具

Shell脚本学习指南之文本处理工具介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本学习指南之查找与替换介绍

Shell脚本学习指南之查找与替换介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

分享shell编程中的几个小技巧

分享shell编程中的几个小技巧,学习shell编程的朋友可以看下
收藏 0 赞 0 分享

linux中常用脚本和函数分享

这linux中经常需要用到的一些脚本与函数,这里简单的分享下,方便需要的朋友
收藏 0 赞 0 分享

关于Shell脚本效率优化的一些个人想法

很想像之前的一片Mysql全面优化详解那样子写一篇全面的优化文章,但是苦于没有相关书籍参考,也没有发现网络牛人有总结帖或文章之类的,所以就根据个人理解和经验写一些能优化程序效率的东西吧。这篇是不敢称全面的
收藏 0 赞 0 分享

shell 基本计算、逻辑运算、位运算详解

Shell 提供大量的基本运算操作,在脚本中非常有用。Shell 对您提供的算术表达式求值,执行运算展开式,此时使用得出的结果替换表达式
收藏 0 赞 0 分享

hbase shell基础和常用命令详解

HBase是一个分布式的、面向列的开源数据库,源于google的一篇论文《bigtable:一个结构化数据的分布式存储系统》
收藏 0 赞 0 分享
查看更多