开 发者学堂课程【Shell 脚本进阶:Shell脚本复杂参数处理】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址 :https://developer.aliyun.com/learning/course/799
Shell脚本
复杂参数处理<p style="text-align:center;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38
);line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-
size:19px" class="lake-fontsize-14">
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">目
录:
<p style="text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-size:19px"
class="lake-fontsize-14">一、if-
then语句
<p style="text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">二、<span
style="font-size:19px" class="lake-fontsize-14">条件测试
<p style="text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">三、<span
style="font-size:19px" class="lake-fontsize-14">if<span style="font-size:19px" class="lake-
fontsize-14">语句高级特性
<p style="text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38);line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-size:19px"
class="lake-fontsize-14">四、<span style="font-size:19px" class="lake-fontsize-
14">case语句
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-size:19px"
class="lake-fontsize-14">
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">一、if-then语句
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">1、if-then语句格式
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">2、当if后面的命令,运行后的退出状态码是0时
,then后面的命令会被执行
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px"><img
src="https://ucc.alicdn.com/pic/developer-ecology/4ce7e9c1843b4225b9d52cd09d7ba30b.png" class="image
lake-drag-image" alt="image.png" title="image.png" style="width:589px" /><span class="lake-card-
margin-top lake-card-margin-bottom"><img src="https://ucc.alicdn.com/pic/developer-
ecology/8f16b4b52fdb44e085a2a7aeecf84110.jpeg" class="image lake-drag-image" alt="image.jpeg"
title="image.jpeg" style="width:141.5px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">
38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-
size:19px" class="lake-fontsize-14">二、条件测试(test命令)
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">1、test命令提供了在if-then语句中测试不同条件的途径。
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">2、如果test命令中列出的条件成立,
返回的退出状态码为0,反之为1。
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">3.、test命令的等价写法[ ] (方括号两边一定要加上空格号)
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">4、提供的三类判断条件
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">(1)数值比较
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top
lake-card-margin-bottom"><img src="https://ucc.alicdn.com/pic/developer-
ecology/6e13407545b749e2b8cfc22fc17614f8.png" class="image lake-drag-image" alt="image.png"
title="image.png" style="width:865px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">(2)字符串比较
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px"><img
src="https://ucc.alicdn.com/pic/developer-ecology/928ba2b7ada043039186ebf7fded2c3b.png" class="image
lake-drag-image" alt="image.png" title="image.png" style="width:865px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">(3)文件比较
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top lake-card-margin-
bottom"><img src="https://ucc.alicdn.com/pic/developer-ecology/401437dfb2784cc99f819def8fd376e6.png"
class="image lake-drag-image" alt="image.png" title="image.png" style="width:865px" />
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">5、格式
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top lake-card-margin-
bottom"><img src="https://ucc.alicdn.com/pic/developer-ecology/65617f5df8e24b9294cafdd584644016.png"
class="image lake-drag-image" alt="image.png" title="image.png" style="width:856.5px" />
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">补充:复合条件测试
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top
lake-card-margin-bottom"><img src="https://ucc.alicdn.com/pic/developer-
ecology/691f3b38d09b4487bd8f69ed8f57b42a.png" class="image lake-drag-image" alt="image.png"
title="image.png" style="width:719.5px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">1:只有当条件1满足时,才会进行条件2的测试
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">2:只要任意条件返回值为0,复合条件返回值为
0
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">三、
if语句高级特性
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38, 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">1、双括号
(())
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">(1)支持高级数学表达式的计
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">(2)命令格式: (( expression ))
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">(3)expression可以是数学赋值或比较表达式
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">示例:
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top lake-card-margin-
bottom"><img src="https://ucc.alicdn.com/pic/developer-ecology/47815a219792412ab77c567a9c98f31f.png"
class="image lake-drag-image" alt="image.png" title="image.png" style="width:500px" />
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">2、双方括号 [[ ]]
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">(1)支持针对字符串比较的高级特性
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">(2)命令格式:[[
expression ]]
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38);line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">(3)除了标准的字符
串比较,还支持模式匹配
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb(38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">示例:
<pstyle="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-
height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-card-margin-top
lake-card-margin-bottom"><img src="https://ucc.alicdn.com/pic/developer-
ecology/4fbcc6b4c9384cb887399e43aa6f1686.png" class="image lake-drag-image" alt="image.png"
title="image.png" style="width:508.5px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">
<p style="text-align:justify;text-indent:28pt;font-size:14px;color:rgb( 38 ,38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span style="font-
size:19px" class="lake-fontsize-14">四、sase语句
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px">1、常代替if-then-elif语句用于对某个变量有大量判断语句的场景
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">2、格式:
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px"><img
src="https://ucc.alicdn.com/pic/developer-ecology/57c7d68a331942a8b78337dc6439f7e2.png" class="image
lake-drag-image" alt="image.png" title="image.png" style="width:525px" />
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-
spacing:0.05em;outline-style:none;margin:0px">示例:
<p style="text-align:justify;text-indent:21pt;font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-
style:none;margin:0px"><img
src="https://ucc.alicdn.com/pic/developer-ecology/0173472697944cf1b431046ae0dbadf8.png" class="image
lake-drag-image" alt="image.png" title="image.png" style="width:500px" />