- 判断命令返回值
if shell-command <args...>; then
...
fi
这个判断逻辑是shell-command的返回值是否为0:
- 为0:则判断逻辑为真,走then分支
- 非0:则判断逻辑为假,走else分支
- 如何取反
if ! shell-command <args...>; then
...
fi
- 如何根据命令stdout输出来判断,而非命令返回值
if test $(shell-command <args...>) == "<string>" ; then
...
fi
- 如何判断多个命令与或关系的结果
if shell-command1 <args...> || shell-command2 <args...>; then
fi
if shell-command1 <args...> && shell-command2 <args...>; then
fi
- 条件判断也可以用在while的条件上,语法是一样的。
while shell-command1 <args...>; do
...
done
网友评论