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

admin3年前云主机75

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

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

相关文章

免费海外cdn缓存怎么清理

免费海外CDN缓存怎么清理随着互联网的普及和全球化,越来越多的网站和应用程序开始面向全球提供服务。这意味着用户可以从世界各地访问同一个网站或应用程序,但也意味着在全球范围内提供高效的响应变得更加复杂。...

深港iplc节点

深港IPLC节点什么是IPLC节点IPLC,即国际专线,是一种全球覆盖的高速数据通信服务,支持各种全球性应用程序解决方案。IPLC节点是指IPLC网络中的物理节点,是承载IPLC业务的关键组件之一。深...

美国cn2服务器租用如何选择服务商

美国CN2服务器租用如何选择服务商对于需要托管网站或者企业应用的用户来说,选择一个可靠、高性价比的服务器托管服务商是至关重要的。而在选择美国CN2服务器租用服务商时,用户需要考虑到以下因素。1. 服务...

swapidc系统下载

SwapIDC系统下载引言SwapIDC系统是一种全新的云计算系统,可以帮助用户轻松管理和运营云端的资源。与传统的IDC系统相比,SwapIDC具有更高的弹性、更低的成本和更加便捷的操作。下载过程用户...

临汾云主机

临汾云主机什么是临汾云主机?临汾云主机是一种基于云计算技术的虚拟化主机服务。相比于传统的物理服务器,它更具有灵活性和可扩展性,可以随时根据业务需求自由地调整资源配置。此外,临汾云主机还具有高可用性、高...

云上生活,盼你所盼,hecs云服务器助你快人一步

随着科技的飞速发展,云上生活的概念正逐渐进入我们的日常生活。hecs云服务器致力于为用户提供高质量的云服务,从而提升生活效率。本文将从四个方面详细阐述云上生活、hecs云服务器的优势及其对用户的帮助。...