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

admin3年前云主机102

在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中mv命令使用详解(移动文件或者将文件改名)(linux中mv命令的用法)

mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者内容介绍。1.命令格式:  &...

双线VPS服务器优势揭秘:性能、稳定、安全再升级

随着互联网技术的发展,VPS服务器在实际应用中越来越常见。本文在介绍双线VPS服务器的基础上,针对其性能、稳定性、安全性等方面进行了详细的分析。通过对比分析,可以看出双线VPS服务器具有更好的性能、更...

宁波服务器租用有哪些途径?

宁波服务器租用有哪些途径?宁波服务器租用的途径有以下几种:通过宁波当地的互联网服务提供商(ISP)租用服务器,这些ISP通常提供多种规格和价格的服务器,可以根据客户需求选择。在一些大型的IT公司或数据...

国外站群vps

什么是国外站群VPS?国外站群VPS是指一种通过虚拟化技术实现多站点托管的虚拟专用服务器。它采用国外数据中心的服务器资源,让用户可以在一个VPS系统上同时托管多个网站。这种方式可以降低成本,提高管理效...