I'm retrieving a host-name from VMware Tools and trying to evaluate if two variables are present in my script:
selection=2
# Check if hostname is present in guestinfo
hostname="$(vmtoolsd --cmd "info-get guestinfo.startup.hostname")"
if [[ "$selection" = 2 && ! -z ${hostname+x} ]]
then
echo "Args present."
else
echo "Args NOT present."
fi
Regardless of whether hostname value is set in the VMX config file, the if statement returns "Args present."
I believe this is because the vmtoolsd command is executed, meaning the 'hostname' variable is not null. Unsure how to fix.
What is wrong?
First, clean up your tests-- don't use ! -z, when you have a -n.
In addition, if you are adding x to hostname, it will always be true (it will always return x by itself). Bash never needs the +x, get rid of it.
if [[ "$selection" = 2 && -n $hostname ]]; then
echo "Args present."
else
echo "Args NOT present."
fi