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

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.

Related

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.

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

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

Use string as bash variable name in alternative value expansion [duplicate]

This question already has answers here:
How can I look up a variable by name with #!/bin/sh (POSIX sh)?
(4 answers)
Closed 7 years ago.
How can I use the value of one variable as the name of another variable in an alternative value expansion (${var+alt}) in bash?
I would think that
#!/bin/bash
cat='dog'
varname='cat'
if [ -z ${`echo "${varname}"`+x} ]; then
echo 'is null'
fi
should be roughly equivalent to
#!/bin/bash
if [ -z ${dog+x} ]; then
echo 'is null'
fi
but when I try to do this, I get
${`echo "${cat}"`+x}: bad substitution
I guess part of the problem is that the subshell doing the command substitution doesn't know about $varname anymore? Do I need to export that variable?
My reason for doing this is that I learned from this answer how to check if a variable is null, and I'm trying to encapsulate that check in a function called is_null, like this:
function is_null {
if [ $# != 1 ]; then
echo "Error: is_null takes one argument"
exit
fi
# note: ${1+x} will be null if $1 is null, but "x" if $1 is not null
if [ -z ${`echo "${1}"`+x} ]; then
return 0
else
return 1
fi
}
if is_null 'some_flag'; then
echo 'Missing some_flag'
echo $usage
exit
fi
I'm not sure if I understand your problem.
If I got, what you need is eval command.
$ cat='dog'
$ varname='cat'
$ echo ${varname}
cat
$ eval echo \$${varname}
dog

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 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