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

admin3年前云主机17

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

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

相关文章

range回源是什么

Range回源是什么?在互联网发展的今天,网站运营商越来越关注用户的访问体验,提高网站服务的可用性和可靠性已经是所有网站运营商的必然选择。而CDN作为一种分布式的网络服务,大大提高了用户访问的速度和稳...

chatgpt底层实现的原理是什么

chatgpt底层实现的原理是什么ChatGPT是一种生成式语言模型,采用开放式的生成方式来构建对话系统,允许用户与机器产生自然流动的对话。ChatGPT的底层实现原理主要包括以下几个方面:1.语言模...

服务器面板推荐

选择一个合适的服务器面板服务器面板是一个用于管理服务器的应用程序。它允许您以图形用户界面的形式管理和监控您的服务器。但是,选择一个合适的服务器面板有时会是一个挑战。以下是一些可以帮助您提高效率和准确性...

虚拟主机试用指南:选购、配置、使用

随着互联网的发展,虚拟主机已经成为了很多网站托管的首选方案。但是对于普通用户来说,虚拟主机的选购、配置和使用可能会比较困难。因此,本文将从四个方面详细介绍虚拟主机的选购、配置和使用,以帮助读者更好地上...

福州高防服务器能防御哪些类型的网络攻击?福建高防服务器租用配置怎么选择?

如今很多互联网公司都会选择高防服务器代替普通服务器,因为高防服务器在配置、网络资源等方面都明显好于普通服务器,更重要的是,其防御网络攻击能力强于普通服务器。那么,福州高防服务器能防御哪些类型的网络攻击...

好云优惠码

好云优惠码介绍及使用方法好云优惠码是一种优惠方式,可以让购物者在购买商品时减少一定的购物费用。好云优惠码通过提供给用户一个特定的代码,购物者在结算时消费的钱数即可打折。如何获取好云优惠码?获取好云优惠...