2013-12-13 65 views
1

我想写一个函数比较一个命令的输出并运行一个循环,直到它什么都不返回。当我运行它时,它的行为就像条件未被满足,并且脚本永远停留在while循环中,即使如果我在提示符下运行该命令,它也不会返回任何内容。任何帮助是极大的赞赏。机器是Mac OS 10.9,这就是为什么su的语法有点时髦。Bash命令输出比较空文本字符串

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 
} 

在提示符处运行将返回以下内容。

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

谢谢!

回答

3

[ and ]需要在[之后和]之前的空格。

但是你可以重写你的while条件为:

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