我和 Vagrant 有一个项目。有时我在主机上工作。我从客人那里工作的其他一些人。我有一个只能在Guest 内部工作的脚本。我想检查我是在 vagrant 机器内部还是外部,以避免出现错误消息。这就是我正在做的:
if ! [ -d "/home/vagrant/project-folder" ]; then
echo 'Cannot execute current script outside the vagrant';
exit
fi;
除了检查文件夹是否存在,还有另一种方法可以检查我是否在 Vagrant 机器中?
最佳答案
您可以使用传递给脚本的环境变量:
config.vm.provision "shell", path: "script.sh", env: {"IN_VAGRANT" => "true"}
然后在脚本中:
if [-z "$IN_VAGRANT" ]; then
echo 'Cannot execute the current script outside the vagrant';
exit 1
fi
或者只是检查
logname
?if [ `logname` = "vagrant" ]; then
echo 'Cannot execute the current script outside the vagrant';
exit 1
fi
关于bash - 如何检查 bash 脚本是在 Vagrant 机器内部还是外部执行的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241938/