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

admin3年前云主机74

在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、密码复杂度:使用不同字符类型组合的长密码...

淮安服务器

淮安服务器——稳定可靠的计算机系统淮安服务器是一种用于存储、管理和保护数据的计算机系统。相比于个人电脑,服务器在功能和性能上拥有更高的要求,而淮安服务器则是在这些方面表现优异的一种产品。优势一:高可靠...

云储存解析:未来企业数据重心的转移

云储存已经成为现代企业重要的技术手段,它在许多方面都改变了传统的数据存储方式。本文将详细分析云储存解析:未来企业数据重心的转移,引出读者的兴趣,并给读者提供背景信息。一、云储存与企业数据云储存指的是把...

义乌网页制作

义乌网页制作义乌是一个拥有众多小型企业和外贸市场的城市,数万家小型制造企业在这里开展业务,而网页制作对于这些企业来说非常重要。因此,义乌的网页制作行业也在不断发展。义乌网页制作公司义乌网页制作公司不仅...

ssl加速技术有哪些优势(ssl加速技术有哪几种)

ssl加速技术的优势有:1、安全性高,能保证网络上数据传输的安全性;2、支持各种应用层协议,能为任何基于TCP等可靠连接的应用层协议提供安全性保证;3、部署简单,不需要安装额外的客户端软件,能鉴别网站...

开启智能制造新时代:恒为科技智能工厂亮相

智能制造正逐渐成为制造业的主流趋势,并呼唤着更多的智能工厂。本文以恒为科技智能工厂亮相为中心,分析了其在智能制造领域的突出表现,并阐述了智能制造对于企业的意义和未来发展趋势。一、智能制造与恒为科技智能...