Why is [ "$foo"=="$bar" ] always true in bash? [duplicate] - bash

This question already has answers here:
Why equal to operator does not work if it is not surrounded by space?
(4 answers)
Closed 5 years ago.
I tried to compare user input between to string
Here is my code
Encode="Encode"
Decode="Decode"
printf "Enter name of file: "
read fileName
printf "Encode Or Decode: "
read EncOrDec
if [ "$Encode"=="$EncOrDec" ]; then
printf "Encode Nice\n"
elif [ "$Decode"=="$EncOrDec" ]; then
printf "Decode Nice\n"
else
printf "Nothing\n"
fi
Its always go to the Encode statement, Why?.
And how to fix it

In bash, spaces count. Replace:
if [ "$Encode"=="$EncOrDec" ]; then
With:
if [ "$Encode" = "$EncOrDec" ]; then
Without spaces, bash is just testing whether the string "$Encode"=="$EncOrDec" is empty or not. Since it is never empty, the then clause is always executed.
Also, as a minor detail, when using [...], the use of = for string equality is POSIX standard. Bash accepts == but == is not standard and won't be reliably portable.
The same applies to the elif line. Replace:
elif [ "$Decode"=="$EncOrDec" ]; then
With:
elif [ "$Decode" = "$EncOrDec" ]; then

Related

Shell always evaluates both factors of &&? [duplicate]

This question already has answers here:
Why does 'test -n' return 'true' in bash?
(2 answers)
Closed 2 years ago.
When $thing is null this quits...
thing=`command_that_could_return_null`
echo "thing is" $thing
if [ -z $thing ]; then exit; fi
...but this...
if [ -n $thing ] && [ $thing = "special_value" ]; then
do_the_special_thing
fi
comes out with
[: =: unexpected operator
I don't understad.
When $thing is null the first factor of the && evaluates to false and therefore the second should not be evaluated, but it appears that it is being evaluated?
I guess it's just a feature of sh that I have to write this as two ifs?
If you fix your quote problem (which #CharlesDuffy mentioned) - your bottom 3 lines work just fine:
if [ -n "$thing" ] && [ "$thing" = "special_value" ]; then
do_the_special_thing
fi
Also, you referred to variables being "null", but the relevant term is a variable that is "unset". In Bourne shell terminology, "null" refers to the empty string.

What's the meaning of if [ -z "$SPARK_HOME" ]; then [duplicate]

This question already has answers here:
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I'm looking at the following code:
if [ -z $2 ]; then
echo "usage: ...
(The 3 dots are irrelevant usage details.)
Maybe I'm googling it wrong, but I couldn't find an explanation for the -z option.
-z string: True if the string is null (an empty string)
See https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
-z
string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
test -z returns true if the parameter is empty (see man sh or man test).
The expression -z string is true if the length of string is zero.

Bash Scripting if statement [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 8 years ago.
I don't know why this isn't working. Please help!
#!/bin/bash
clear
echo "Enter an option"
read $option
if ("$option" == 1) then
echo "Blah"
fi
I tried like this
if ("$option" -eq 1) then
I can't see why the if statement isn't being run. All I want to do is check what the user entered and do something depending on the value entered.
The syntax for an equality check is:
if [[ $option == 1 ]]; then
echo "Blah"
fi
Or, for compatibility with older non-bash shells:
if [ "$option" = 1 ]; then
echo "Blah"
fi
In either one, the whitespace is important. Do not delete the spaces around the square brackets.
That is not the syntax for an if statement in bash. Try this:
if [ "$option" = "1" ]; then
echo "Blah"
fi
I'm not sure where you got your syntax from...try this:
if [ "$option" -eq 1 ]; then
In the shell, [ is a command, not a syntactic construct.
Alternatively, you can use an arithmetic context in bash:
if (( option == 1 )); then
Bash if uses [ or [[ as test constructs, instead of parenthesis which are used in other languages.

what is wrong with my unix scripts [duplicate]

This question already has answers here:
what is wrong with my unix script
(5 answers)
Closed 9 years ago.
#!/bin/bash
while echo -n "Player's name?"
read name
[ $name != 'ZZZ' ]
do
searchresult=$(grep [$name] playername)
if [ $searchresult=0 ]
then
echo -n "if See target (T/t) or team name (M/m)?"
read team
read target
while [ [ $target!=T ] || [ $team!=M ] ]
do
echo "Please enter only T or M."
done
if $target=T
then
grep [ $name ] targetselected
else
grep [ $name ] teamselected
fi
else
echo 'no such player'
fi
done
echo You are now exited search
error msg - line 10: [: too many arguments, what is that mean?
Not sure which line 10 in posted code you are pointing to but I found few issues with your posted code as
the line while [ [ $target!=T ] || [ $team!=M ] ] should be
while [ $target!="T" ] || [ $team!="M" ]
Also, if $target=T should accordingly be if $target="T"
The two lines:
searchresult=$(grep [$name] playername)
if [ $searchresult=0 ]
are very wrong. If you are trying to determine if the grep succeeds, it is best to do:
if grep -q "$name" playername; then ...
Putting grep in $() and assigning to searchresult has you comparing the output of grep rather than checking its exit status. And the spaces that are missing around the = in the second line are very wrong. Assuming searchresult is a string that contains no whitespace and only alphanumeric characters, the line if [ $searchresult=0 ] will always evaluate to true because the string "$searchresult=0" is always non-empty. (If searchresult is the empty string then $searchresult=0 is the string =0 which is non-empty, so the test succeeds.) It is highly unlikely that you intend to be testing whether or not that string is empty. The exact error that you are seeing indicates that the grep is assigning a value to searchresult which contains whitespace. In that case [ $searchresult=0 ] (without double quotes around $searchresult) is being parsed by [ as multiple arguments (split on the whitespace in $searchresult), and the error is telling you that there are too many arguments.

What does -z mean in Bash? [duplicate]

This question already has answers here:
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I'm looking at the following code:
if [ -z $2 ]; then
echo "usage: ...
(The 3 dots are irrelevant usage details.)
Maybe I'm googling it wrong, but I couldn't find an explanation for the -z option.
-z string: True if the string is null (an empty string)
See https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
-z
string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
test -z returns true if the parameter is empty (see man sh or man test).
The expression -z string is true if the length of string is zero.

Resources