Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
# . : match any character
# * : previous char is present zero or more times
# \. : match a dot
if [[ "$CheckMainServerOutput" =~ .*ms.* ]] || [[ "$CheckMainServerOutput" =~ .*32\.43\.26.* ]]; then
BTW, a pattern matching looks like:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then
# . : match any character
# * : previous char is present zero or more times
# \. : match a dot
if [[ "$CheckMainServerOutput" =~ .*ms.* ]] || [[ "$CheckMainServerOutput" =~ .*32\.43\.26.* ]]; then
BTW, a pattern matching looks like:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then
No, it doesn't. Pattern matching uses =~, and uses an actual
regex-engine, so to match a literal period .*32\.43\.26.* is the
correct form.
Try this:
Code:
if [[ "$CheckMainServerOutput" =~ *ms* || "$CheckMainServerOutput" =~ *32.43.26* ]]; then
if [[ "$CheckMainServerOutput" =~ *ms* || "$CheckMainServerOutput" =~ *32.43.26* ]]; then
Is that what you intended, Tinkster? AFAIK it means "$CheckMainServerOutput" contains *m followed by any number of the character s or *32 followed by any character ...
On further reflection, the reason bash generated syntax errors on interpreting
Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then
is that the =~ comparison operator is not valid in the [ ... ] test form, only in the [[ ... ]] form.
Is that what you intended, Tinkster? AFAIK it means "$CheckMainServerOutput" contains *m followed by any number of the character s or *32 followed by any character ...
My intention was to quote the string the OP used. I think I succeeded.
Whether the original string is what the OP really wanted I don't know.
Quote:
Originally Posted by catkin
On further reflection, the reason bash generated syntax errors on interpreting
Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then
is that the =~ comparison operator is not valid in the [ ... ] test form, only in the [[ ... ]] form.
...
[[ expression ]]
...
When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching. If the shell
option nocasematch is enabled, the match is performed without
regard to the case of alphabetic characters. The return value
is 0 if the string matches (==) or does not match (!=) the pat-
tern, and 1 otherwise. Any part of the pattern may be quoted to
force it to be matched as a string.
...
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 expres-
sion and matched accordingly (as in regex(3)). The return value
is 0 if the string matches the pattern, and 1 otherwise.
...
Pattern Matching
Any character that appears in a pattern, other than the special pattern
characters described below, matches itself. The NUL character may not
occur in a pattern. A backslash escapes the following character; the
escaping backslash is discarded when matching. The special pattern
characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
* Matches any string, including the null string.
...
So I was right, this is pattern matching:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.