bash if中的like子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9871883/
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-09 21:51:28  来源:igfitidea点击:

like clause in if

bashshellif-statement

提问by shantanuo

The following is working as expected.

以下按预期工作。

for schema in `mysql -e"show databases"`
do
if [[ $schema = 'test' ]];then
echo $schema
fi
done

But when I need to select all values starting with test, the following does not work for obvious reasons...

但是,当我需要选择以 test 开头的所有值时,由于显而易见的原因,以下内容不起作用......

if [[ $schema = 'test%' ]];then

I want to select the other values like test_db, test123 etc.

我想选择其他值,如 test_db、test123 等。

回答by beny23

You could use bash regexes:

您可以使用 bash 正则表达式:

if [[ $schema =~ ^test ]]; then

回答by paxdiablo

You canuse wildcards (regular expressions) in bash:

可以在以下位置使用通配符(正则表达式)bash

pax> if [[ test123 =~ ^test.* ]] ; then echo yes ;fi
yes

From the bashmanpage:

bash联机帮助页:

An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)).

The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2.

If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. Any part of the pattern may be quoted to force it to be matched as a string.

Substrings matched by parenthesized sub‐expressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

可以使用额外的二元运算符 =~,其优先级与 == 和 != 相同。使用时,运算符右侧的字符串被视为扩展正则表达式并进行相应匹配(如 regex(3) 中所示)。

如果字符串与模式匹配,则返回值为 0,否则为 1。如果正则表达式在语法上不正确,则条件表达式的返回值为 2。

如果启用了 shell 选项 nocasematch,则执行匹配时不考虑字母字符的大小写。可以引用模式的任何部分以强制将其作为字符串进行匹配。

正则表达式中与括号中的子表达式匹配的子字符串保存在数组变量 BASH_REMATCH 中。索引为 0 的 BASH_REMATCH 元素是与整个正则表达式匹配的字符串部分。索引为 n 的 BASH_REMATCH 元素是与第 n 个带括号的子表达式匹配的字符串部分。

回答by glenn Hymanman

Plain shell patterns will work too, since this does not really require a regular expression:

普通 shell 模式也可以使用,因为这并不真正需要正则表达式:

if [[ $schema == test* ]]; ...

See the documentation for [[ ]]-- http://www.gnu.org/software/bash/manual/bashref.html#index-g_t_005b_005b-57

请参阅文档[[ ]]-- http://www.gnu.org/software/bash/manual/bashref.html#index-g_t_005b_005b-57

回答by William Pursell

Another option is to move the check and do:

另一种选择是移动支票并执行以下操作:

for schema in $( mysql -e"show databases" | grep ^test )