这就是我想要做的:
#!/bin/bash set -e # I can't remove this line! if [ docker inspect foo ]; then echo container is present else echo container is absent fi
可能吗? docker inspect foo在容器不存在时返回退出代码1,当它出现时返回0.
现在我明白了:
-bash: [: too many arguments
如果要在“if”语句中运行该命令,则可以这样执行:
原文链接:https://www.f2er.com/bash/384825.htmlif docker inspect foo then echo container is present else echo container is absent fi
如果需要,可以省略所有换行符或用分号替换.这也有效:
if docker inspect foo; then echo container is present; else echo container is absent; fi
如果你想要更紧凑的东西,你可以使用这种语法:
docker inspect foo && echo container is present docker inspect foo || echo container is absent docker inspect foo && echo container is present || echo container is absent
&安培;&安培;如果第一个命令成功,则运行第二个命令. ||如果第一个命令失败,则运行第二个命令.最后一行使用两种形式.