Bash string comparision [duplicate] - bash

This question already has answers here:
Bash regex =~ operator
(3 answers)
Regex matching in a Bash if statement
(4 answers)
How can I match a string with a regex in Bash?
(6 answers)
Simple logical operators in Bash
(5 answers)
Closed 2 years ago.
Please help.
I'm trying to compare string1 against string2 in a Bash script.
I can do the easy bit of:-
if [[ $string1 == $string2 ]]
then
yippee
fi
What I'm having trouble with is the syntax for when
"the$string1" == $string2 or "a$string1" == $string2
or
$string1 == the$string2 or $string1 == a$string2
I assume it's something like:-
if [[ $string1 == $string2 || "(a|the)$string1" == $string2 || $string1 == "(a|the)$string2" ]]
But it's not and I cannot seem to find the answer. (I'm obviously asking the wrong question!)
Thanks for any help.
PS I'd rather not use any external progs such as awk etc.

You might want:
if [[ $string1 == *"$string2"* ]]; then
echo "string1 contains string2"
elif [[ $string2 == *"$string1"* ]]; then
echo "string2 contains string1"
fi
Within [[...]] the == operator is a pattern matching operator.
Ref: 6.4 Bash Conditional Expressions
For specifically an optional "a" or "the" prefix:
[[ $string1 == ?(a|the)"$string2" || $string2 == ?(a|the)"$string1" ]]
That uses bash's extended patterns, see 3.5.8.1 Pattern Matching

Related

Conditional binary operator expected: syntax error near `$1' [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
I am trying to run simple code block.
It gives error on fourth line as "syntax error near `$1'"
=~ means Matches Regular Expression
How should I use '$1' variable with this operator?
Here is my code:
if [[ $1 -gt 3 ]] && [[ $1 -lt 7 ]]
then
echo "$1 is between 3 and 7"
elif [[ $1 =~ "Jeff"]] || [[ $1 =~ "Roger" ]] || [[ $1 =~ "Brian" ]]
then
echo "$1 works in the Data Science Lab"
else
echo "You entered: $1, not what I was looking for.."
fi
Very funny indeed. You've typed the first condition in that line as [[ $1 =~ "Jeff"]], so without a space between "Jeff" and ]] bash interprets them as a single string, which is obviously not your pattern, and the whole parse fails and line structure crashes. If you add that space:
if [[ $1 =~ "Jeff" ]] || [[ $1 =~ "Roger" ]] || [[ $1 =~ "Brian" ]]
then it works... seemingly...

Error in shell script: Integer expression expected [duplicate]

This question already has answers here:
Shell equality operators (=, ==, -eq)
(4 answers)
Closed 4 years ago.
i am studying shell script right now. I started to learn how to work with more complex if statements. What's wrong with this code bellow? I read other similar questions here in stackoverflow, but i couldnt resolve my problem. Now im verifying if the first, second or third argument is null. In the future i pretend to verify based in some regex or something like that.
Thanks!!
The code (line 9):
if [ "$1" -eq "" ] || [ "$2" -eq "" ] || [ "$3" -eq "" ] then ...
line 9: [: : integer expression expected line 9: [: : integer
expression expected line 9: [: : integer expression expected
-eq performs an arithmetic comparison between two numbers. Use = for string comparisons. Or better yet, use [[ and ==.
[[ $1 == "" ]]
[ "$1" = "" ]
You can also use -z and -n to directly test whether a value is empty/non-empty.
[[ -n $value ]] # [[ $value != "" ]]
[[ -z $value ]] # [[ $value == "" ]]
use [[ and ]] for the more modern / complex operators. This is a bashism, so beware.

bash [[ [a] == [a] ]] not true? square bracket affect compare result

Anyone know why this happens? Is this a bug of bash?
x='mnt:[4026532411]'
[[ $x == $x ]] && echo OK
I am expecting result OK, but it did not.
Of course, this works
[[ "$x" == "$x" ]] && echo OK
But as I know, bash [[ ]] have a merit that no need to quote var when compare.
x='a b'
[[ $x == $x ]] && echo OK
works.
Ironical things is
x='mnt:[4026532411]'
[[ $x != $x ]] && echo Oh my god
result is Oh my god
The unquoted right-hand side of == and != is treated as a pattern, not a literal string. mnt:[4026532411] will match mnt: followed by exactly one of 0, 1, 2, 3, 4, 5, or 6, since the patterns mnt:[4026532411] and mnt:[0123456] are equivalent. To match the lieral string, you need to quote the expansion.
x='mnt:[4026532411]'
[[ $x == "$x" ]] && echo OK
What you are seeing is do do this sentence from the bash man page:
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, as if the extglob
shell option were enabled.
As you may already know, [...] in the shell allows matching from a
group of characters. That is, given the files:
$ ls
fileA fileB fileC fileD
Running ls file[AB] will yield:
fileA fileB
So in your expression, mnt:[1234] is interpreted in a similar
fashion.

Using if conditions in shellscripting [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
read A
if [["$A" == 'Y' -o "$A" =='y']]
then echo "YES"
else echo "NO"
fi
I am very new to shell scripting.basically, i am trying to check if the input is Y or y.
I am getting the following error which I am not able to debug.
solution.sh: line 2: [[Y: command not found
Thanks.
You are missing space after [[
The if should be like
if [[ "$A" == 'Y' || "$A" == 'y' ]]
The [[ ]] is an extended test command, like any command it should be separated from others by spaces
Add proper space inside the square brackets and after the ==:
read A
if [ "$A" == 'Y' -o "$A" == 'y' ]; then
echo "YES";
else
echo "NO";
fi
Note that to use -o you ought to use test command, which uses single brackets.
If you use [[ ]], with double brackets, you must use || instead of -o:
read A
if [[ "$A" == 'Y' || "$A" == 'y' ]]; then
echo "YES";
else
echo "NO";
fi

bash string equality [duplicate]

This question already has answers here:
What is the difference between operator "=" and "==" in Bash?
(2 answers)
Closed 7 years ago.
In bash, what's the difference, if any, between the equal and double equal test operators?
[[ "a" = "a" ]] && echo equal || echo not-equal
[[ "a" == "a" ]] && echo equal || echo not-equal
[[ "a" = "b" ]] && echo equal || echo not-equal
[[ "a" == "b" ]] && echo equal || echo not-equal
results in:
equal
equal
not-equal
not-equal
There's no difference, == is a synonym for = (for the C/C++ people, I assume). See here, for example.
You could double-check just to be really sure or just for your interest by looking at the bash source code, should be somewhere in the parsing code there, but I couldn't find it straightaway.

Resources