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

admin3年前云主机40

在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. 企业信息管理:可以对企业信息进行全面的管理,包括员工信息、客户信息、产品信息、订单信息...

虚拟主机云(虚拟主机云空间运营商在郑州找哪个公司比较好)

文章目录:1、云服务器、虚拟主机、云虚拟主机、网站空间的区别是什么?2、请问云虚拟主机哪家好3、选择云虚拟主机需要考虑哪些因素?4、虚拟主机和云主机的区别是什么5、虚拟主机和云主机的最大区别是什么?6...

南非和埃及服务器:哪家更适合你的业务需求?

随着互联网技术的不断发展和普及,越来越多的企业和个人需要在全球范围内进行数据交流和服务器托管。本文将从网络质量、稳定性、数据隐私和价格四个方面对南非和埃及服务器进行比较,以帮助读者选择适合自己业务需求...

如何提高租用香港服务器的安全性(提升香港服务器租用安全。)

一、服务器的基础安全设置服务器的基础安全设置主要包括系统密码设置、开启防火墙、禁止root用户SSH登录等。对于登录密码,应该遵循密码的复杂度要求,包含大小写字母、数字和特殊字符,并定期更换密码。开启...

T4:新能源汽车的创新应用与发展趋势

一、技术创新1、新能源汽车电池技术的发展:随着电池技术的不断创新,新能源汽车的续航能力和安全性能得到极大提升。比如,纳米硅电极技术、钛酸锂电池技术、固态电池技术等,都能加快电池充电速度、提高能量密度和...

挑选香港服务器的小技巧有哪些

选择香港服务器的小技巧有哪些香港作为亚洲的商业中心和IT创新中心,拥有非常优秀的网络和通信基础设施,成为了大量国际和本地企业以及互联网服务供应商的首选数据中心位置。选择香港服务器也非常受到欢迎。本文将...