'VBoxManage guestcontrol' 到 运行 shell 来宾脚本

'VBoxManage guestcontrol' to run shell script on guest

我有 VirtualBox 虚拟机,运行 是一个可以通过本地主机和转发端口访问的服务器。 我需要 运行 一些 shell 脚本并根据结果实现一些业务逻辑。

我试过以下命令作为示例:

VBoxManage guestcontrol <UUID> exec --image /bin/sh --username <su username> --password <su password> --wait-exit --wait-stdout --wait-stderr -- "[ -d /<server_folder>/ ] && echo "OK" || echo "Server is not installed""

但我收到错误消息:

/bin/sh: [ -d <server_folder> ] && echo : No such file or directory

上面的语法有什么问题?

首先确保 VBoxManage.exe 在您的路径中!

其次,你必须小心你的报价。您使用了:

"[ -d /<server_folder>/ ] && echo "OK" || echo "Server is not installed""

最外层的引号必须使用单引号:

'[ -d /<server_folder>/ ] && echo "OK" || echo "Server is not installed"'

最后你必须在你的参数前添加一个-c(调用/bin/sh -c '...')。

完整命令:

VBoxManage guestcontrol <UUID> exec --image /bin/sh --username <su username> --password <su password> --wait-exit --wait-stdout --wait-stderr -- -c '[ -d /<server_folder>/ ] && echo "OK" || echo "Server is not installed"'