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

admin3年前云主机51

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

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

相关文章

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

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

服务器带宽监测工具有哪些(30个汉字以内,只把拟定的新标题返回出来)

一、服务器带宽监测工具的作用服务器带宽监测工具是一种可以监测服务器带宽使用情况的软件工具。其主要作用是帮助管理员了解服务器带宽的使用情况,从而可以针对具体情况进行优化和调整,避免由于带宽不足导致服务器...

vpccdn怎么样

VPCCDN是什么VPCCDN的全称是Virtual Private Cloud Content Delivery Network,是一种基于云计算的CDN加速服务。它将传统CDN的缓存服务和云计算的...

商标注册费用

什么是商标注册费用?商标注册是指,在国家规定的范围内对商标进行注册,获得商标注册证,形成法律保护,使其在商品市场上不被盗用和侵犯,从而保障自身的品牌价值和商业利益。而商标注册费用则是指在此过程中需要缴...

揭秘香港网站变慢的痛点,拖累用户体验与业务数据!

一、网络瓶颈香港作为全球金融中心,有着非常发达的互联网基础设施。然而,由于海量数据流量的不断涌入,很多网络服务商的带宽已经不能满足需求。尤其是在繁忙的时段,很多用户会发现访问速度变得特别慢,甚至无法访...

印尼服务器租用需要注意些什么?

印尼服务器租用需要注意些什么?租用印尼服务器时,需要注意以下几点:了解服务器提供商的背景和信誉度:在选择服务器提供商之前,需要对其进行充分的调查和研究。建议选择有良好声誉、服务质量高的公司。确定服务器...