I feel confused about how to use "=~" when I read the info of bash(3.2.25) at rhel5.5
# 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 ? And why the second command return true,apparently it should return 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 optionnocasematch' (see the description ofshopt' 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 variableBASH_REMATCH'. The element ofBASH_REMATCH' with index 0 is the portion of the string matching the entire regular expression. The element ofBASH_REMATCH' with index N is the portion of the string matching the Nth parenthesized subexpression.
Aucun commentaire:
Enregistrer un commentaire