mpmackenna
mpmackenna

Reputation: 443

Bash compare output of command to empty text string

I am trying to write a function that compares the output of a command and runs a loop until it returns nothing. When I run it, it acts like the condition has not been met and the script is forever stuck in the while loop, even though if I run the command at the prompt it returns nothing. Any help is greatly appreciated. Machine is Mac OS 10.9 which is why the syntax of su is a little funky.

stopvm ()
{
    su macadmin -c "VBoxManage controlvm tjfog acpipowerbutton"
    while [`su macadmin -c 'VBoxManage list runningvms | grep tjfog'` != ""]
    do
        echo "Waiting for the VM to shutdown"
        sleep 3
    done
}

Running at the prompt returns the following.

bash-3.2# su macadmin -c VBoxManage list runningvms | grep tjfog
bash-3.2#

Thanks!

Upvotes: 1

Views: 196

Answers (1)

anubhava
anubhava

Reputation: 785146

[ and ] require spaces inside after [ and before ].

But you can rewrite your while condition as:

while su macadmin -c 'VBoxManage list runningvms | grep -q tjfog'
do
    echo "Waiting for the VM to shutdown"
    sleep 3
done

Upvotes: 3

Related Questions