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

admin3年前云主机39

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

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

相关文章

wordpress开启gzip

以WordPress开启gzip在今天的网络世界中,速度是关键。搜索引擎和用户都希望网站能够快速加载,并能够快速浏览。幸运的是,有一种技术可以加速您的WordPress网站加载速度,那就是开启gzip...

企业邮箱推荐:选择哪个品牌最好?

作为企业必备的工具之一,企业邮箱是企业与外界沟通的重要环节。然而,挑选一个适合自己企业的品牌并不容易。本文从用户体验、安全性、可定制性和价格四个方面为您详细介绍企业邮箱品牌的推荐,希望能帮助您更好地选...

影响国外服务器价格的因素有哪些

背景介绍随着互联网的快速发展,云计算和大数据等应用越来越广泛,国外服务器需求量也愈发庞大。而服务器价格的高低直接影响着企业云计算和大数据等应用的成本,因此了解影响国外服务器价格的因素对企业非常重要。数...

分析Google和百度搜索引擎的习惯问题(谷歌和百度对比研究分析)

Google搜索引擎习惯Google作为全球最大的多语言搜索引擎在发展历史过程中形成了自己的网页收录习惯,也建立起自己的一套标准。研究Goolge收录网页的习惯有利于更好迎合Google搜索引擎的口味...

服务器在美国受到保护

服务器在美国受到保护当今,数据已经成为了企业最宝贵的财富之一。为了确保数据的安全,企业在数据管理上花费了大量的精力和财力。而保护企业的数据从源头开始,就是要保证存储数据的服务器的安全。因此,选择一个安...

万网域名注册攻略:教你快速注册域名及避免常见问题

一、选择合适的域名后缀1、域名后缀的选择首先应该看您的网站性质,如果是公司官方网站,那么、.cn、.net等后缀比较合适。如果是非营利性质的组织或机构,可以选择.org或.edu后缀。如果是个人网站,...