加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 当nocasematch关闭时,为什么case语句区分大小写?

发布时间:2020-12-15 22:50:23 所属栏目:安全 来源:网络整理
导读:鉴于以下内容: $echo $BASH_VERSION4.2.10(1)-release$shopt | fgrep casenocaseglob offnocasematch off$case A in [a-z]) echo TRUE;; esacTRUE 我希望大写字母A不应该匹配[a-z]的小写字符类,但确实如此.为什么这场比赛没有失败? 解决方法 您不能以这种
鉴于以下内容:

$echo $BASH_VERSION
4.2.10(1)-release

$shopt | fgrep case
nocaseglob      off
nocasematch     off

$case A in [a-z]) echo TRUE;; esac
TRUE

我希望大写字母A不应该匹配[a-z]的小写字符类,但确实如此.为什么这场比赛没有失败?

解决方法

您不能以这种方式可靠地使用破折号.如果我不使用短划线,它按预期工作:

$bash --version
GNU bash,version 4.2.10(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation,Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY,to the extent permitted by law.
$shopt -s nocasematch
$case A in [abc]) echo TRUE;; esac
TRUE
$shopt -u nocasematch
$case A in [abc]) echo TRUE;; esac
$

但是使用破折号,无论nocasematch的设置如何,它都会打印为TRUE.

Bash在这里进行模式匹配.查看this section of the reference manual,其中使用连字符MIGHT将[a-z]解释为[A-Za-z]!它告诉您如何获得传统解释(将LC_COLLATE或LC_ALL设置为C).基本上,您的默认语言环境是按字典顺序排序.参考手册很好地解释了事情.

附录

好的,我有一份成绩单给你.

$shopt -u nocasematch
$case A in [a-z]) echo TRUE;; esac
TRUE
$shopt -s nocasematch
$case A in [a-z]) echo TRUE;; esac
TRUE
$LC_ALL=C
$shopt -u nocasematch
$case A in [a-z]) echo TRUE;; esac
$shopt -s nocasematch
$case A in [a-z]) echo TRUE;; esac
TRUE

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读