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

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.

Related

How to check if array element is substring of string in Bash [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 2 years ago.
This question does not answer my question.
I have a script that checks the existence of specific environment variables and prints them with their values. Now I want to mask the values of password variables (containing SECRET, PW, PASSWORD, KEY in its name, e.g. CLIENT_SECRET) with ****.
Currently I have a script like this:
expected_env_vars=("CLIENT_ID" "CLIENT_SECRET" "BACKEND_KEY" "BACKEND_NAME")
suppress_env_vars_with_substring=("SECRET" "PASSWORD" "PW" "KEY")
for env_var in "${expected_env_vars[#]}"; do
if [[ -z "${!env_var}" ]]; then
echo "Environment variable \"$env_var\" not defined"
exit 1
else
# Perform check if an element of $suppress_env_vars_with_substring is substring of $env_var
echo "$env_var=${!env_var}...OK"
fi
done
Question
How to check if an array element is substring of a string?
You may use this script with a grep:
expected_env_vars=("CLIENT_ID" "CLIENT_SECRET" "BACKEND_KEY" "BACKEND_NAME")
suppress_env_vars_with_substring=("SECRET" "PASSWORD" "PW" "KEY")
for env_var in "${expected_env_vars[#]}"; do
if [[ -z "${!env_var}" ]]; then
echo "Environment variable \"$env_var\" not defined"
exit 1
else
# Perform check if an element of $suppress_env_vars_with_substring is substring of $env_var
printf '%s=' "$env_var"
grep -qFf <(printf '%s\n' "${suppress_env_vars_with_substring[#]}") <<< "$env_var" &&
echo '****' || echo "${!env_var}"
fi
done
It does not matter, whether or not the operands to be tested belongs to an array or not. The general form is
[[ $x == *$y* ]] && echo "$y is a substring of $x"
You can substitute for $x and $y and parameter expansion you like, including array elements.

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.

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

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