脚本编程进阶指南
1. 条件语句
在脚本编程中,我们经常需要根据不同的条件来执行不同的操作。常见的条件语句使用方式包括利用命令的退出状态。每个命令运行后都会返回一个退出状态码,通常情况下,如果命令执行顺利,退出状态码为 0;如果出现错误,则为其他数字。可以使用如下的 if 语句:
if command
then
# if command exits normally do this
else
# if command resulted in an error do this
fi
另一种处理多个潜在条件的方式是使用 case 语句。 case 语句会检查一个变量的值,并根据不同的值做出不同的响应。一般来说, 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 prev
超级会员免费看
订阅专栏 解锁全文
1498

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



