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

admin3年前云主机77

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

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

相关文章

phpcms管理员不存在怎么办(phpcms管理员密码忘了)

php零基础到就业直播视频课:进入学习API 文档、设计、调试、自动化测试一体化协作工具:点击使用phpcms管理员不存在怎么办?phpcmsV9正常安装后,后台管理员不存在? phpmyadmin无...

mlklink

为什么使用 MLKlink?MLKlink是一个强大的短链接生成工具,它可以大大简化您在社交媒体等平台上的分享链接的过程。现在,您不再需要复制和粘贴长长的URL地址,MLKlink可以通过将长网址转换...

vue如何导入weui

如何在Vue中导入Weui框架Vue是一个流行的JavaScript框架,它使创建Web应用程序变得更加容易。但是,Vue并不像其他框架那样提供使用现成的UI组件库。因此,我们需要使用一个UI组件库来...

南非约翰尼斯堡云服务器用处有哪些呢?

南非约翰尼斯堡云服务器有以下用途:托管网站-南非约翰尼斯堡云服务器可以托管您的网站,并提供高度可靠和稳定的基础设施,确保您的网站能够随时在线。数据备份和存储-南非约翰尼斯堡云服务器可以作为数据备份和存...

【TikTok直播的条件】- 如何成为一名优秀的TikToker?

TikTok作为目前社交平台中新晋的一员,其直播功能正吸引着越来越多的用户。但如何成为一名优秀的TikToker?本文将从四个方面介绍TikTok直播的条件,包括内容策略、人设塑造、直播技巧和互动留存...

阿里云数据可视化:找到信息的Schlüssel。

一、数据可视化的概念和意义数据可视化是将数据以图形的方式呈现给用户,简化数据处理和理解,是数据分析的重要工具。数据可视化可以帮助企业精准决策,提高效率,减少工作量,同时也可以让个人更好的了解和处理大量...