从 VBoxManage list runningvms 中获取的当前运行中虚拟机列表,遍历一个个关闭,可以指定关闭方式:pause|resume|reset|poweroff|savestate|acpipowerbutton|acpisleepbutton,默认为 savestate。
#!/bin/sh
act="savestate"
args="pause|resume|reset|poweroff|savestate|acpipowerbutton|acpisleepbutton"
# check arguments
if [ $# -gt 1 ]; then
echo "Syntax error: Unkown argument '$2'\nUsage: ${0##*/} [ $args ]"
exit 1
fi
if [ -n "$1" ]; then
if [ -z $(echo $args | grep -o -w $1) ]; then
echo "Syntax error: Invalid parameter '$1'\nUsage: ${0##*/} [ $args ]"
exit 1
fi
fi
for vm in $(VBoxManage list runningvms | grep -o '".*"'); do
# delete first char
cmd="VBoxManage controlvm ${vm#?}"
# delete last char, and use parameter $1 if not empty
cmd="${cmd%?} ${1:-$act}"
echo "$cmd"
# excute command
$cmd
done
exit 0