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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
...
[[ 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
Of course it is ... and so is =~ (coincidentally described in the same bit
of bash's man-page that you quoted), which you claimed, by way of different example
But I read all over the internet that the syntax $(command) is the correct way of executing a command.
It is the correct way to capture the output of a command when modified to my_variable=$(command 2>&1) (the 2>&1 captures the stderr output as well as the stdout).
The way to run a command is simply command
This makes sense when you think of a bash script being a automated command prompt session. At the command prompt, you run a command by typing it and pressing Enter.
Thank you for your help. I notice that execution of the command hangs... I think it is related to the service that is being started. That's why I now execute another bash script which executes this command. This way my "main" bash script is further executed. It works well.
Another question :
suppose CheckNumber="[space][space][space]7"
I want to get the last 2 ciphers of this string. If there is only one cipher, I want to delete white spaces. I do the following :
Number=${CheckNumber:0:2} # get last 2 characters
${Number//[[:space:]]} # delete white spaces
if [[ "$Number"=="0" ]]; then
echo "number 0"
else
echo "number more than 0"
fi
I notice that the variable $Number is always empty.
So my if-then-else also fails.
See what I'm doing wrong ?
Last edited by jonaskellens; 02-18-2012 at 04:59 AM.
I want to get the last 2 ciphers of this string. If there is only one cipher, I want to delete white spaces. I do the following :
Code:
Number=${CheckNumber:0:2} # get last 2 characters
${Number//[[:space:]]} # delete white spaces
if [[ "$Number"=="0" ]]; then
echo "number 0"
else
echo "number more than 0"
fi
I notice that the variable $Number is always empty.
So my if-then-else also fails.
See what I'm doing wrong ?
Add the command set -xv just before the problem section to better see what the script is doing.
Bash wants spaces either side of comparison operators such as == and you want the numeric comparison operator -eq, not the string comparison operator ==.
... the code gets the first two characters as the set -xv trace shows.
If you want to use echo to display the value of variables you must double quote them or bash will strip leading/training whitespace and convert all embedded whitespace sequences to a single space. It is also helpful to put markers at beginning and end so you can see any leading/training whitespace:
Code:
echo "Number: '$Number'"
The else echo in ...
Code:
if [[ "$Number"=="0" ]]; then
echo "number 0"
else
echo "number more than 0"
fi
... is wrong. The else case is triggered when $Number is not equal to the string 0 -- or would be if the == in the test expression had space either side of it so it was a comparison operator and not a component of the string "$Number"=="0" which is actually "==0" at run time as the set -xv trace shows.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.