Bash中不区分大小写的比较 [英] Case insensitive comparison in Bash
本文介绍了Bash中不区分大小写的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在不区分大小写的while语句中编写比较.基本上,我只是想简化以下内容,以便对用户显示是或否的问题提示...
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[ $yn == "y" | $yn == "Y" | $yn == "Yes" | $yn == "yes" ] ; do
解决这个问题的最佳方法是什么?
What would be the best way to go about this?
推荐答案
shopt -s nocasematch
while [[ $yn == y || $yn == "yes" ]] ; do
或:
shopt -s nocasematch
while [[ $yn =~ (y|yes) ]] ; do
注意
-
[[
是bash关键字,与[
命令类似(但功能更强大).参见 http://mywiki.wooledge.org/BashFAQ/031 和Note
[[
is a bash keyword similar to (but more powerful than) the[
command. See http://mywiki.wooledge.org/BashFAQ/031 and 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_REMATCH
can 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中不区分大小写的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文