What does this shell test achieve

独自空忆成欢 提交于 2021-02-05 09:25:32

问题


I have a very simple question that I can't answer. In shell, what would this command do:

test -d $VIRTUAL_ENV || virtualenv $VIRTUAL_ENV

It seems like it tests if the virtualenv directory exists, but I dont understand what if does with that information. Will it always create the virtualenv afterwards, or would it only do it if it doesn't already exist?


回答1:


The || is the OR condition. Hence, this will test if $VIRTUAL_ENV directory exists. If not, it will run virtualenv $VIRTUAL_ENV.

Other examples:

$ test -d /tmp || echo "yes"
$
$ test -d /tmpblabla || echo "this dir does not exist"
this dir does not exist
$ test -d /tmp && echo "/tmp exists" || echo "yes"
/tmp exists



回答2:


It tests if the directory $VIRTUAL_ENV exists and otherwise creates it using virtualenv



来源:https://stackoverflow.com/questions/19768796/what-does-this-shell-test-achieve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!