如何检查 bash 脚本是在流浪机器内部还是外部执行的?
How to check if a bash script is executed inside or outside a vagrant machine?
我有一个 Vagrant 的项目。有时我在 Host 工作。其他一些我在 Guest 工作。我有一个只能在来宾内部运行的脚本。我想检查我是在流浪机器内部还是外部,以避免出现错误消息。这就是我正在做的事情:
if ! [ -d "/home/vagrant/project-folder" ]; then
echo 'Cannot execute current script outside the vagrant';
exit
fi;
除了检查文件夹是否存在之外,还有另一种检查我是否在流浪机器内的方法吗?
您可以使用传递给脚本的环境变量:
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
我有一个 Vagrant 的项目。有时我在 Host 工作。其他一些我在 Guest 工作。我有一个只能在来宾内部运行的脚本。我想检查我是在流浪机器内部还是外部,以避免出现错误消息。这就是我正在做的事情:
if ! [ -d "/home/vagrant/project-folder" ]; then
echo 'Cannot execute current script outside the vagrant';
exit
fi;
除了检查文件夹是否存在之外,还有另一种检查我是否在流浪机器内的方法吗?
您可以使用传递给脚本的环境变量:
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