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

admin3年前云主机38

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

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

相关文章

升级换代:从前置服务器到面向客户端的服务化架构

正文:一、前置服务器的局限性随着数字经济时代的到来,越来越多的企业开始加快数字化转型的步伐。作为数字化转型的重要组成部分,前置服务器已经逐渐暴露出其不足之处。首先,前置服务器无法实现资源动态扩展,这限...

高防御的扬州服务器该怎么选?

高防御的扬州服务器该怎么选?选择扬州服务器时,如果您需要高防御能力,可以考虑以下因素:带宽:首先要确保所选服务器带宽足够高,以应对可能出现的DDoS攻击等恶意网络流量。DDoS防护技术:其次,需要了解...

TStor软件

TStor软件介绍在当前数据存储领域,TStor软件是一个备受关注的解决方案。该软件可以为用户提供可靠性高、可扩展性强、数据传输速度快等优异特性。实现了数据存储的完整性和安全性,并支持多种数据格式,使...

Dedecms标签优化方法大全:提高网站内容可见度及搜索引擎排名

Dedecms标签优化方法大全是一篇零售电商网站优化的技术文章。本文将详细介绍如何利用Dedecms标签优化网站内容,以提高其可见度和提高搜索引擎排名。这篇文章将为您提供更深入的了解,以便更好地优化您...

优劣比较:租用美国服务器与国内服务器的利弊对比

一、成本差异美国服务器的租用相对于国内的服务器,价格更加优惠,因此更受中小型企业的青睐。这主要是因为美国数据中心的定位不同,他们通过精简的流程和规模化的运营,实现成本的控制,可以提供更具有竞争力的价格...

如何快速访问海外服务器?掌握这些技巧让你畅游世界!

一、VPN1、什么是VPNVPN(Virtual Private Network),即虚拟专用网络,是一种通过公共网络(如互联网)进行安全的私人网络连接技术。使用VPN可以从家里或公司安全地远程访问文...