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

admin3年前云主机50

在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、通过用户行为数据以及商家数据,中原云商电商平台为商家提供个性化的推广策略。平台不仅能够将商家的产品定向到目标受众,同时也能够根据不同用户的需求和偏好做出针对性的广告推荐。2、平台...

主机安全:如何保护你的计算机免受网络攻击?

随着互联网的快速发展,个人电脑的安全问题日益成为人们关注的热点。好的安全措施可以防止黑客和病毒攻击,保护您的计算机数据的安全。本文将从防火墙、安全软件、密码安全和系统升级四个方面详细探讨如何保护您的计...

centos7如何关闭mongodb服务

Centos7如何关闭MongoDB服务介绍MongoDB是一种非关系型数据库,是一种文档数据库。Centos7的操作系统中,不同的应用程序需要使用不同的端口,为了避免端口争用,推荐在一些不用的应用程...

国外免实名服务器购买如何选择

引言在互联网时代,网络安全是一项非常重要的问题。与此同时,要保护个人隐私,选择一款好的网络加密工具也变得尤为重要。在国外,很多人喜欢使用免实名服务器购买来保护自己的网络数据。但是,在网络加密工具市场上...

一个虚拟主机多少钱

虚拟主机价格大揭秘对于很多需要建立自己网站的用户来说,选择虚拟主机就是一个非常好的选择。虚拟主机由于价格低廉,可以满足大多数用户的需求。那么,虚拟主机的价格大概是多少呢?下面我们就来详细探讨一下虚拟主...