Bash 中不区分大小写的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15558101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:56:54  来源:igfitidea点击:

Case insensitive comparison in Bash

bashsyntax

提问by user2150250

I'm trying to write a comparison in a while statement that's case insensitive. Basically, I'm simply trying to shorten the following to act on a yes or no question prompt to the user ...

我正在尝试在不区分大小写的 while 语句中进行比较。基本上,我只是想缩短以下内容,以便对用户提出是或否的问题提示...

while[ $yn == "y" | $yn == "Y" | $yn == "Yes" | $yn == "yes" ] ; do

What would be the best way to go about this?

解决这个问题的最佳方法是什么?

回答by Gilles Quenot

shopt -s nocasematch
while [[ $yn == y || $yn == "yes" ]] ; do

or :

或者 :

shopt -s nocasematch
while [[ $yn =~ (y|yes) ]] ; do

Note

笔记

  • [[is a bash keyword similar to (but more powerful than) the [command. See http://mywiki.wooledge.org/BashFAQ/031and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
    Unless you're writing for POSIX sh, we recommend [[.
  • The =~operator of [[evaluates the left hand string against the right hand extended regular expression (ERE). After a successful match, BASH_REMATCHcan be used to expand matched groups from the pattern. Quoted parts of the regex become literal. To be safe & compatible, put the regex in a parameter and do [[ $string =~ $regex ]]
  • [[是一个类似于(但比)[命令更强大的 bash 关键字。请参阅http://mywiki.wooledge.org/BashFAQ/031http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
    除非您是为 POSIX sh 编写,否则我们推荐[[.
  • 所述=~的操作者[[求值针对右手左手串扩展正则表达式(ERE)。匹配成功后,BASH_REMATCH可用于从模式中扩展匹配组。正则表达式的引用部分变为文字。为了安全和兼容,将正则表达式放在参数中并执行[[ $string =~ $regex ]]

回答by Riot

No need to use shopt or regex. The easiest and quickest way to do this (as long as you have Bash 4):

无需使用 shopt 或正则表达式。执行此操作的最简单快捷的方法(只要您有 Bash 4):

if [ "${var1,,}" = "${var2,,}" ]; then
  echo "matched"
fi

All you're doing there is converting both strings to lowercase and comparing the results.

您所做的就是将两个字符串都转换为小写并比较结果。

回答by chepner

Here's an answer that uses extended patterns instead of regular expressions:

这是使用扩展模式而不是正则表达式的答案:

shopt -s nocasematch
shopt -s extglob
while [[ $yn = y?(es) ]]; do
  ...
done

Note that starting in version 4.1, bashalways uses extended patterns inside the [[ ... ]]conditional expression, so the shoptline is no necessary.

请注意,从 4.1 版开始,bash始终在[[ ... ]]条件表达式中使用扩展模式,因此该shopt行不是必需的。

回答by Kent

try this one too:

也试试这个:

 [[ $yn =~ "^[yY](es)?$" ]] 

回答by jirislav

I prefer using grep -Eor egrepfor short:

我更喜欢使用grep -Eegrep简称:

while egrep -iq '^(y|yes)$' <<< $yn; do
  your_logic_here
done

Here you have explanations from grepmanual:

这里有grep手册中的解释:

-i, --ignore-case

Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)

-q, --quiet, --silent

Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.)

-i, --ignore-case

忽略 PATTERN 和输入文件中的大小写区别。(-i 由 POSIX 指定。)

-q, --quiet, --silent

安静的; 不要向标准输出写入任何内容。 如果找到任何匹配项,则立即以零状态退出,即使检测到错误。另请参阅 -s 或 --no-messages 选项。(-q 由 POSIX 指定。)