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

admin3年前云主机105

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

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

相关文章

万维视界:数字化时代下人类与自然的新挑战

数字化时代的到来使得人类能够更加方便地获取和利用信息资源,但是这也带来了一系列新的挑战,特别是在环境保护方面,数字化时代下的人类和自然之间的关系变得越来越复杂。本文将从四个方面对万维视界:数字化时代下...

日本独立ip服务器建站有哪些优势

Japan Independent IP Server建站的优势随着互联网的不断发展,建立一个网站已经成为许多企业不可或缺的一部分。在选择服务器时,很多人都会选择日本独立IP服务器,那么,这种服务器有...

中心化证书服务器:从安全、效率、可靠性三个角度分析

一、安全1.1 保密性中心化证书颁发机构(CA)涉及到的数据是机密且重要的。从保密性角度看,中心化证书服务器需要做到以下步骤:1)安全认证:必须通过认证与授权的措施来确保仅授权用户才能访问服务器。2)...

高防服务器是如何进行防御的?福建高防大带宽服务器有什么用途?

网站安全问题一直以来都是广大用户比较关心的问题,很多公司或者企业就因为网络攻击的影响带来巨大的损失,因此越来越多的人都开始关注高防服务器,那么高防服务器是如何进行防御的?高防服务器能够适用于很多应用场...

resellerclub虚拟主机(ResellerClub虚拟主机更换ip)

文章目录:1、resellerclub云虚拟主机怎么样?有没人用过?2、虚拟主机和主机,有什么不同?3、哪家免备案虚拟主机好,而且便宜点的?4、有一台VPS可以做什么?resellerclub云虚拟主...

国内高防和香港高防分别有什么区别(国内高防与香港高防有何不同?)

一、数据中心基础设施国内高防服务商多以自建数据中心为主,将高防策略与数据中心集成,实现付费用户的安全服务。而香港高防主要是租用华人万达、Equinix等数据中心进行高防服务。在设备硬件方面,国内高防使...