1.if语句
1.语句结构:
if condition #当 condition 这个条件成功,也就是退出状态为0时,才会执行后面的statements,否则不会执行。
then
statements
fi
也可以写成:
if condition;then #分号一定要写
statements
fi
2.示例1:
当 if后的命令成功时:
当无法识别if后的 命令:
示例2:
2.if else 语句
该语句的语法特点和 if 语句类似。
if condition #当 condition 成立,会执行 statement1,否则 执行 statement2
then
statement1else
statement2
fi
还是拿上面的最后一个例子修改一下:
注意:当分支数目比较多时,可以用if elif else 结构,他的格式为:
(if 和 elif 后面都必须加 then)
if condition1 #当 condition1 成立,会执行statement1,否则尝试condition2
then
statement1
elif condition2 #当 condition2 成立,会执行statement2,否则尝试condition3,依此类推
then
statement2
elif condition3
then
statement3
else ##当 所有condition 都不成立,会执行statementn
statementn
fi
例题: