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

admin3年前云主机26

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

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

相关文章

linux远程端口怎么改

Linux怎么远程改变端口要远程修改Linux某个服务的端口,需要在控制台使用命令进行修改。通常情况下,服务的配置文件位于/etc目录下,可以通过修改此配置文件进行端口的修改。下面将详细介绍如何在Li...

廊坊云主机

什么是廊坊云主机?廊坊云主机是指位于廊坊地区的云计算服务商提供的云服务器租用服务。云服务器的基础设施和硬件设备由服务商负责,用户只需通过互联网进行远程管理和使用,无需自己建设和维护服务器,大大降低了企...

hostslim优惠码

HostSlim优惠码HostSlim是一家荷兰的主机服务提供商,提供多种不同类型的主机方案,适合不同业务需求的网站使用。本文将介绍HostSlim的优惠码,并探讨这些优惠码对于用户的意义。HostS...

ubuntu如何查看usb信息

Ubuntu如何查看USB信息介绍在Ubuntu系统中,我们经常需要查看USB设备的信息,例如USB设备的名称、型号、容量等等。本文将介绍如何在Ubuntu系统中查看USB设备的信息。使用lsusb命...

便宜国外云服务器租用如何选择服务商

云服务器的优势随着云计算技术的不断发展,云服务器越来越受欢迎。相比传统服务器,云服务器有许多优势,如可扩展性强、弹性伸缩、资源共享等。不过,国内云服务器价格相对较贵,因此购买国外云服务器貌似成为不少企...

互联网信息服务新趋势:个性化推荐服务解析

随着互联网技术的不断发展和普及,个性化推荐服务成为了互联网信息服务的重要趋势。本文将从用户需求、算法技术、商业模式和政府监管四个方面分析个性化推荐服务的解析,探讨其发展趋势。一、用户需求1、个性化服务...