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

admin3年前云主机22

在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)》来自互联网同行内容,若有侵权,请联系我们删除!

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

相关文章

dede城市分站插件

了解Dede城市分站插件Dede城市分站插件是一款应用于dedecms建站系统的插件,它可以快速帮助你建立起一个城市分站系统,使你的网站更加贴近用户群体需求。该插件易于安装和使用,可以实现多城市数据及...

租用韩国云服务器时有哪些注意事项(韩国云服务器租用须知,小心坑!)

一、价格租用云服务器首先需要考虑价格问题,价格方面主要需要关注以下几点:1.实际收费:在选择云服务器时,不要被低廉的宣传价格所迷惑,需要了解实际收费情况,包括服务费、带宽费和流量费等。2.套餐选择:不...

备案大礼包

备案大礼包什么是备案?备案是指将一个网站的域名和服务器信息提交给国家相关部门并审核通过的过程。备案的目的是维护网络安全和信息管理秩序,对于未备案的网站,将被视为非法网站并可能面临惩罚。备案的重要性备案...

优客云:云计算时代的企业智能化解决方案

一、云计算基础设施云计算是当前企业数字化转型的重要支撑之一,优客云早在2010年便开始向企业提供云计算基础设施服务,持续为客户提供稳定高效的云服务器、云存储等基础设施服务。除此之外,优客云还提供了完善...

“破解迷思:香港服务器与海内服务器差异的全面解析”

一、网络环境与基础设施建设网络环境和基础设施建设是香港服务器与海内服务器最显著的区别之一。香港作为国际金融、商贸和航运中心,相对于内地城市具有天然优势,网络环境和基础设施建设也更加完善。香港的互联网接...

国内能够防御ddos的高防服务器都有哪些?泉州BGP高防服务器租用哪家好?

对于一个网站运营者来说,遭遇ddos攻击已经是家常便饭,为了针对ddos攻击,很多服务器厂商都专门推出ddos高防服务器和ddos流量攻击防护。那么大家都知道国内能够防御ddos的高防服务器都有哪些?...