如何检查在流浪汉机器内部或外部执行bash脚本?

时间:2017-05-29 11:57:00

标签: bash vagrant

我和Vagrant有一个项目。有时我在主持人工作。我在宾客工作的其他一些人。我有一个只在Guest内部工作的脚本。我想检查我是在流浪汉机器内还是在外面,避免错误消息。这就是我在做的事情:

if ! [ -d "/home/vagrant/project-folder" ]; then
    echo 'Cannot execute current script outside the vagrant';
    exit
fi;

还有另一种检查我是否在流浪汉机器内的方法,而不是检查文件夹是否存在?

1 个答案:

答案 0 :(得分:2)

您可以使用传递给脚本的环境变量:

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  
相关问题