Shell 条件语句

shell 条件语句

if

1
2
3
4
5
6
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi

case

1
2
3
case word in
[ [(] pattern [| pattern]…) command-list ;;]…
esac

示例

1
2
3
4
5
6
7
8
9
echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat) echo -n "four";;
man | kangaroo ) echo -n "two";;
*) echo -n "an unknown number of";;
esac
echo " legs."

备注:

  1. *)相当于其他语言的default

  2. ;;相当于break, ;&相当于fall through

  3. |分割多个模式,相当于or


Shell 条件语句
https://yxibng.github.io/2022/02/25/Shell/2022-02-25-shell 条件语句/
作者
yxibng
发布于
2022年2月25日
许可协议