I have an if statement that always executes and I'm not sure why.
Here's the code:
var=dom1
if(var=$(ssh root@192.168.0.200 /usr/sbin/xm li | grep dom1 | awk '{print $1}')); then
then the code to be executed if true.
The problem is, the stuff after the if statement always executes no matter if its true or not.
The command in the if statement works because I've tried it manually.
xm li just gives all the running virtual machines and the rest of it makes sure that the only thing returned is the name of the virtual machine. When dom1 is running on the other machine it returns dom1. When dom1 isn't running on the other machine it doesn't return anything.
I'm not sure whats up, and any help would be appreciated. Thank you.
Finishing with an awk
command will fails because you cannot relies of the exit status of awk
(not boolean capable in shell). Instead, just use grep
itself to match with boolean logic. By providing some sample input & matching rules in your original POST, I will help you to do it.
A workaround if you really want to use awk
like in your post:
More over,
var=$(ssh root@192.168.0.200 /usr/sbin/xm li | awk '/dom1/{print $1}')
if [[ $var ]]; then
# ...
fi
that will work if the subcommand have an output. You may test it before.