Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VboxManage列表vms Linux Shell脚本总是输出错误的结果_Linux_Bash_Shell_Virtualbox - Fatal编程技术网

VboxManage列表vms Linux Shell脚本总是输出错误的结果

VboxManage列表vms Linux Shell脚本总是输出错误的结果,linux,bash,shell,virtualbox,Linux,Bash,Shell,Virtualbox,我做错了什么。我相信每个人都应该清楚我想做什么 #/usr/bin/env sh echo -e "What VM?" read vname if [ '`VboxManage list vms | grep -c "$vname"`' != 0 ]; then echo exists fi 任何人都有建议。它应该告诉我虚拟机是否存在。首先,你的shebang是错误的 试试这个代码 #!/usr/bin/env sh echo -e "What VM?" read -r vname i

我做错了什么。我相信每个人都应该清楚我想做什么

#/usr/bin/env sh
echo -e "What VM?"
read vname
if [ '`VboxManage list vms | grep -c "$vname"`' != 0 ]; then
    echo exists
fi

任何人都有建议。它应该告诉我虚拟机是否存在。

首先,你的shebang是错误的

试试这个代码

#!/usr/bin/env sh
echo -e "What VM?"
read -r vname
if [ "$(VboxManage list vms | grep -c "$vname")" != 0 ]; then
    echo exists
fi

单引号阻止整个VboxManage的扩展。。。命令-它只是被视为一个静态字符串。但是有一种更简单的方法来测试grep是否找到了匹配项;另外,我在编写代码后注意到,在基于Linux的系统上,由于区分大小写的语法,它无法工作。VBoxManage