Shell脚本复杂参数处理|学习笔记

简介: 快速学习Shell脚本复杂参数处理

开 发者学堂课程【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">

<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">二、条件测试(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语句中测试不同条件的途径。

<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、如果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命令的等价写法[ ] (方括号两边一定要加上空格号)

<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">4、提供的三类判断条件

<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"><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" />

<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">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" />

<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/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" />

<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">(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)除了标准的字符

串比较,还支持模式匹配

<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/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" />

相关文章
|
2月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
377 9
|
2月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
239 2
|
5月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
232 60
|
2月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
8月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
4月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
449 1
|
6月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
152 17
|
6月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
172 4
|
6月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
351 3
|
7月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
581 25