简体   繁体   English

如何使用bash(3.2.25)的附加二进制运算符:=〜

[英]How to use bash(3.2.25)'s An additional binary operator:=~

I feel confused about how to use "=~" when I read the info of bash(3.2.25) at rhel5.5 当我在rhel5.5读取bash(3.2.25)的信息时,我对如何使用“ =〜”感到困惑

# match the IP, and return true
[kevin@server1 shell]# [[ 192.168.1.1 =~ "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ]] && echo ok || echo fail
ok

# add double qoute
# return false, en ... I know.
[kevin@server1 shell]# [[ 192.168 =~ "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ]] && echo ok || echo fail
fail

# remove double qoute
# return ture ? Why ?
[kevin@server1 shell]# [[ 192.168 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && echo ok || echo fail
ok 

So, should I qoute the string to the right of the operator ? 那么,我应该在运算符右边对字符串进行qoute吗?
And why the second command return true,apparently it should return false ! 以及为什么第二个命令返回true,显然它应该返回false!

Here is what the info said: 信息显示如下:

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 regex3)). 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 可以使用附加的二进制运算符=~', is available, with the same precedence as =='和!='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). 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 !='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). 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 !='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). 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' (see the description of shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable !='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). 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启用!='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). 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'(请参阅shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable Builtins ::中的shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable描述), shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH'. shopt' in *Note Bash Builtins::) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions 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 0 is the portion of the string matching the entire regular expression. The element ofBASH_REMATCH' with index 0 is the portion of the string matching the entire regular expression. 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 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. 具有索引N BASH_REMATCH' with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH' BASH_REMATCH' with index 0 is the portion of the string matching the entire regular expression. The element of与第N个带括号的子表达式匹配的字符串部分。

The recommended, most widely compatible way of dealing with regular expression patterns is to declare them separately, in single quotes: 建议的,最兼容的处理正则表达式模式的方法是用单引号分别声明它们:

$ re='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
$ [[ 192.168.1.1 =~ $re ]] && echo ok || echo fail 
ok
$ [[ 192.168 =~ $re ]] && echo ok || echo fail 
fail

Some discussion on the differences in behaviour across bash versions can be found on Greg's Wiki - the take-home message is that using an unquoted variable is the best way to do it. 可以在Greg的Wiki上找到有关bash版本之间行为差异的一些讨论-总结是,使用无引号的变量是实现此目标的最佳方法。

Not relevant to the operator itself, however, your regular expression is not limiting each byte of the IP address being matched to be between 0-255. 与操作员本身无关,但是,您的正则表达式并不限制要匹配的IP地址的每个字节在0-255之间。 That regex will accept IP's such as: 999.999.999.999. 该正则表达式将接受IP,例如:999.999.999.999。

Please do consider using the following: 请考虑使用以下内容:

^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$ ^(([01] \\ d \\ d |?2 [0-4] \\ d | 25 [0-5])\\){3}([01] \\ d \\ d |?2 [O- 4] \\ d | 25 [0-5])$

This will match between 0.0.0.0 and 255.255.255.255. 这将在0.0.0.0和255.255.255.255之间匹配。 I use this is regex in java but i do believe the syntax to be the same. 我在Java中使用的是正则表达式,但我确实相信语法是相同的。 If not please do tell. 如果没有,请告诉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM