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

 更新时间:2013年11月07日 17:38:26   作者:  
在shell脚本中,发现case语句的一个问题。就是指定小写字母[a-z]和大写字母[A-Z]的这种方法不管用了

在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编程基础之认识与学习BASH

    shell编程基础之认识与学习BASH

    本文介绍下,shell基础编程中有关bash的相关知识,有需要的朋友参考学习下
    2013-11-11
  • Shell脚本解压rpm软件包

    Shell脚本解压rpm软件包

    这篇文章主要介绍了Shell脚本解压rpm软件包,用来解压后提取某个包中文件,需要的朋友可以参考下
    2014-06-06
  • shell一键部署Zabbix的实现步骤

    shell一键部署Zabbix的实现步骤

    本文主要介绍了shell一键部署Zabbix的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • linux top命令基本实战

    linux top命令基本实战

    top命令的功能是用于实时显示系统运行状态,包含处理器、内存、服务、进程等重要资源信息,这篇文章主要介绍了linux top命令 实战,需要的朋友可以参考下
    2023-02-02
  • shell脚本中取消重定向的方法实例

    shell脚本中取消重定向的方法实例

    这篇文章主要介绍了shell脚本中取消重定向的方法实例,本文直接给出代码实例,需要的朋友可以参考下
    2015-03-03
  • crontab每10秒执行一次的实现方法

    crontab每10秒执行一次的实现方法

    下面小编就为大家带来一篇crontab每10秒执行一次的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Linux Shell函数返回值

    Linux Shell函数返回值

    这篇文章主要介绍了Linux Shell函数返回值,需要的朋友可以参考下
    2016-11-11
  • 一个简单的linux命令 pwd

    一个简单的linux命令 pwd

    这篇文章主要介绍了一个简单的linux命令pwd,pwd命令用于查看当前工作目录的完整路径,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Linux常用命令与命令缩写整理

    Linux常用命令与命令缩写整理

    这篇文章介绍了Linux的常用命令与命令缩写,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • Shell脚本获取进程的运行时间

    Shell脚本获取进程的运行时间

    这篇文章主要介绍了Shell脚本获取进程的运行时间,需要的朋友可以参考下
    2014-06-06

最新评论