这就是我想要做的:
#!/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”语句中运行命令,请按以下步骤进行操作:
if 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
&&
如果第一个成功,则运行第二个命令。||
如果第一个命令失败,则运行第二个命令。最后一行使用两种形式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句