1

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!

1 Answer 1

3

[ 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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.