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

admin3年前云主机37

在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脚本中case条件控制语句的一个bug分析(shell脚本 case)》来自互联网同行内容,若有侵权,请联系我们删除!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

虚拟主机试用有哪些优势(虚拟主机试用有什么好处)

虚拟主机试用的优势有:1、价格便宜,能降低网站空间与用户之间的维护成本;2、提供快速回复和技术支持服务,能降低网站停机的时间,保证网站持续正常运行;3、操作便捷,提供访问者统计信息和营销工具,能缩短更...

ubuntu桌面系统

Ubuntu桌面系统Ubuntu是一种易于使用和维护的桌面系统,它基于Linux操作系统。它的名字源于非洲南部祖鲁部落的一词,意为“人类之间的互助和善意”。Ubuntu系统不仅易于使用,而且提供了多种...

linux建站教程

准备工作在开始建站前,我们首先需要准备一个Linux服务器,可以选择自己购买或者租用云服务器。这里推荐使用Ubuntu系统,因为它易于安装和维护,同时也有着广泛的社群支持。安装好系统后,我们需要为服务...

什么是域名解析服务器(什么是域名解析服务)

域名解析服务器通常由一台服务器系统构成,它可以接收来自外部 Internet 中众多域名解析请求,并将其转发到域名解析服务器上的域名解析数据库中进行查询,根据查询结果返回给对应的客户端,以实现域名到...

香港服务器价格波动因素分析

随着互联网的发展,服务器成为数字化时代的核心设备,云计算的普及也带动了市场的快速发展。本文着眼于香港服务器市场,分析了影响香港服务器价格波动的因素,综合了国内外研究成果和市场现状,旨在为相关从业人员提...

域名如何解析到空间

域名如何解析到空间在建立一个网站的过程中,一个重要的步骤就是将域名解析到服务器空间上。域名解析到空间后,才能使你的网站在互联网上被访问到。那么,域名如何解析到空间呢?一、获取空间服务器信息在将域名解析...