脚本编程基础与高级语言介绍
1. 条件语句
在脚本编程中,有两种常见的条件语句。
- 利用命令退出状态 :每个命令执行后都会返回一个退出状态,通常顺利执行返回 0,出错则返回其他数字。可以使用如下的 if 语句来根据命令的退出状态执行不同操作:
if command
then
# if command exits normally do this
else
# if command resulted in an error do this
fi
-
case语句 :用于处理多个潜在条件。它根据变量的值做出不同响应,通常比if语句更易于使用,但仅适用于处理可能存储在单个变量中的多个相似结果。示例如下:
case "$caseVar" in
1) command(s) to run if the value of $caseVar is "1" ;;
2) command(s) to run if the value of $caseVar is "2" ;;
dog) command(s) to run if the value of $caseVar is "dog" ;;
*) command(s) to run if none of the previous cases matched, "*" is a
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



