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

admin3年前云主机78

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

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

相关文章

美国高防服务器租用多少钱?美国100G高防服务器地址是多少?

美国高防服务器贵不贵?美国高防服务器是国内防御最高的机房之一,机房的带宽充足。美国拥有高速的光纤线路、完备的网络环境以及专业化的网络管理技术。为客户提供快捷、高质量的服务,美国高防服务器线路稳定,受到...

华为云香港线路

华为云香港线路:打造更稳定的云服务随着云计算的普及,越来越多的企业开始将应用程序和数据转移到云上。香港由于地理位置的优越性和政治稳定性,成为了企业们的首选云服务地点。而华为云则以其稳定的云服务和完善的...

易名域名优选

如何选择优质的域名?以域名优选为例1. 域名的重要性在现如今这个数字时代,互联网已经成为人们生活和工作中不可或缺的一部分。而域名则是一个公司或者个人在互联网上的身份证明,是让人们对你的认知和记忆的一部...

云服务器使用的技巧有哪些(云服务器使用技巧指南)

作为一种高效、灵活、可靠的计算资源,云服务器已经成为了现代企业中不可或缺的一部分。本文将针对云服务器的使用技巧进行详细阐述,从基础设置到网络配置,再到应用部署与监控管理,为大家提供一些实用的参考。通过...

云服务器掉包的原因是什么(云服务器掉包原因剖析)

一、网络拥塞网络拥塞是指网络的总传输能力无法满足网络中所有数据流传输带宽的要求,从而导致数据传输延迟、缓慢、丢失的现象。当数据包在传输过程中遇到网络拥塞时,就会发生掉包现象。网络拥塞主要可能是由于流量...

使用主机空间的常见问题有哪些

主机空间的常见问题有哪些随着网络的发展,越来越多的人开始使用主机空间来搭建自己的网站或进行相关业务的托管。然而,在使用主机空间的过程中,可能会遇到各种各样的问题。那么,主机空间的常见问题有哪些呢?接下...