bash 如何以编程方式检查docker机器是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30758986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if docker machine exists programmatically?
提问by wildsurfer
I'm using docker-machine
to manage my cloud servers. I'm writing some bash scripts to automate some tasks. The question is: "How to check in bash script if docker machine with the specific name already exists?". I need some expression to return true if it exists and false if it doesn't.
我docker-machine
用来管理我的云服务器。我正在编写一些 bash 脚本来自动化一些任务。问题是:“如果具有特定名称的 docker 机器已经存在,如何检查 bash 脚本?”。如果存在,我需要一些表达式来返回 true,如果不存在则返回 false。
Thanks
谢谢
回答by Adrian Mouat
Just run it through grep, if a regexp is good enough for you. For example if you have a machine called foo:
如果正则表达式对您来说足够好,只需通过 grep 运行它。例如,如果您有一台名为 foo 的机器:
$ docker-machine ls -q | grep '^foo$'
Should work and return 0. The caret matches the start of the line and the space avoids partial matches. If it doesn't match you will get a non-zero return code.
应该工作并返回 0。插入符号匹配行的开头,空格避免部分匹配。如果不匹配,您将得到一个非零返回码。
回答by Luís Bianchin
You can use something like the following:
您可以使用以下内容:
docker-machine status some-machine 2> /dev/null || echo "Machine does not exists"
回答by Yogesh_D
Not a scripting guru but I would do a "docker-machine help
", if this command runs and the exit code ($?) is zero, the docker-machine executable is available and functioning.
If the return code is 127 (typically this returned by bash for command not found) or anything other than non-zero, you can assume that either docker-machine is not installed or is not running properly.
不是脚本大师,但我会执行“ docker-machine help
”,如果此命令运行且退出代码 ($?) 为零,则 docker-machine 可执行文件可用且正在运行。如果返回代码是 127(通常是 bash 为找不到命令返回)或任何非零值,您可以假设 docker-machine 未安装或未正常运行。