shell 脚本结构化命令

1. if-then 语句基本使用

if-then 语句的基本格式:

if command
thencommands
elifcommands
elsecommands
fi

bash 的 if 语句会运行 if 后面的命令。如果命令的退出状态码是0(命令运行正确),则运行位于 then 部分的命令。如果是其他值,执行 else 部分的命令。

zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bashif ls dir
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
ls: 无法访问 'dir': 没有那个文件或目录
exec else commands
zzz@ubuntu:~/my_learning$ vim test.sh
zzz@ubuntu:~/my_learning$
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bashif ls .
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
test.sh
exec then commands
zzz@ubuntu:~/my_learning$ 

2. test 命令

if-then 语句判断的是命令的状态码。如果需要测试状态码以外的条件,可以使用 test 命令。如果 test 判断的条件成立 test 将返回退出状态码0,如果不成立返回非零退出状态码。test 命令的基本格式:

test condition

使用 test 命令测试变量是否包含内容,如果变量不为空执行then部分语句,否则执行else部分语句。

zzz@ubuntu:~/my_learning$ vim test.sh
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha="My_Name"
if test $a
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$
zzz@ubuntu:~/my_learning$
zzz@ubuntu:~/my_learning$
zzz@ubuntu:~/my_learning$ vim test.sh
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha=""
if test $a
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
exec else commands
zzz@ubuntu:~/my_learning$ 

3. test 命令的另一种测试方法

这种方式,无需声明 test ,而是使用方括号,要注意左括号右边和右括号左边的空格,否则报错。基本格式如下:

if [ condition ]
thencommands
fi

可以判断的三类条件:

  • 数值比较
  • 字符串比较
  • 文件比较

3.1 数值比较

test 的数值比较只能处理整数,不能处理浮点数。

比较 描述
-eq 相等
-ge 大于等于
-gt 大于
-le 小于等于
-lt 小于
-ne 不等于
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha=4
if [ $a -eq 4 ]
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$ 

3.2 字符串比较

比较 描述
= 相等
!= 不相等
< 小于
> 大于
-n str1 长度是否非0
-z str1 长度是否为0

注意
字符串比较时,需要注意字符串的两个顺序问题:

  1. 大于号和小于号必须使用转义字符 \,否则将被视为重定向符号;
  2. 大于小于顺序和sort命令采用的不同:sort 中大号字母被认为是大于小写字母的;

3.3 文件比较

可以测试Linux文件系统上的文件和目录。

比较 描述
-d file 是否存在且是目录
-e file 是否存在,可以是目录或文件
-f file 是否存在且是文件
-r file 文件是否存在并可读
-s file 文件是否存在并非空
-w file 文件是否存在并可写
-x file 文件是否存在并可执行
-o file 文件是否存在并属当前用户
-G file 文件是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧

3.4 复合条件测试

布尔运算 and , or 。

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

4. if-then 高级特性

bash shell 提供了两项可在 if-then 语句中使用的高级特性:

  • 用于数学表达式的双括号;
  • 用于高级字符串处理功能的双方括号;

4.1 双括号

符号 描述
val++, val–,++val, --val 后增,后减,先增,先减
!, ~ 逻辑求反,位求反
** 幂运算
<<, >> 左位移,右位移
&, | 位布尔和,位布尔或
&&, || 逻辑和,逻辑或
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha=4
if (( $a **2 >10))
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$

注:双括号中的大于小于号不需要转义。

4.2 双方括号

双括号中使用 test 测试中的标准字符串比较。另外,双方括号提供了一个特性——模式匹配,可以使用正则表达式。

zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha="my_name"
if [[ $a = *name ]]
thenecho "exec then commands"
elseecho "exec else commands"
fizzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$ 

5. case 命令

case 命令采用列表格式来检查单个变量的多个值,执行不同的命令。可以通过竖线操作在一行中分隔多个模式。*会捕获所有与已知模式不匹配的值。
基本格式:

case variable in
pattern1) commands;;
pattern2 | pattern3) commands;;
*) default commands;;
esac
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/basha="s2"case $a ins1 | s2)echo "exec s1 or s2 commands";;
s3)echo "exec s3 commands";;
*)echo "exec * commands";;
esaczzz@ubuntu:~/my_learning$ ./test.sh
exec s1 or s2 commands
zzz@ubuntu:~/my_learning$ 

Linux shell 脚本结构化命令 if-then相关推荐

  1. linux SHELL之结构化命令

    SHELL之使用结构化命令 使用if-then语句 Bash代码   if command then commands fi 如果if后面的命令退出状态码=0,那么就执行then 另外一种形式 Bas ...

  2. 使shell用结构化命令

    shell--使用结构化命令 使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻 ...

  3. Linux shell脚本中的命令正确写法

    shell脚本中的命令需加反引号``

  4. linux jq 数组,linux shell脚本 用jq命令在JSON文件肿添加一个map或数组

    导师分配了一个产线日志转换的项目,线上产生的日志是多个文件,可读性不高,需求是把所有的日志转换成JSON文件.每一个字段对应一个值,看起来清晰明了. 之前没写过shell脚本,不知道怎么处理JSON, ...

  5. linux shell脚本使用bc命令实现小数数值计算

    目录 问题背景 思路分析 解决方法 问题背景 通常实际工作中使用shell脚本计算小数加减乘除时,需要将计算结果再保存到变量中. 思路分析 使用bc方法,同时新建变量用反引号执行后面计算命令(键盘左上 ...

  6. 使用Bash编写Linux Shell脚本-7.复合命令

    转载自:http://blog.csdn.net/fox_lht/archive/2010/09/20/5897336.aspx 7.   复合命令 除了最简单的脚本,你很少想要执行每一个命令.执行一 ...

  7. Linux shell 脚本之shift 命令实战

    位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...

  8. Linux Shell脚本编程 --split命令

    linux下文件分割可以通过split命令来实现,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现,非常简单. 在Linux下用split进行文件分割: 模式一:指 ...

  9. Linux Shell脚本_历史命令显示操作时间

    当前用户什么时间执行的什么命令 ① 脚本编写 创建脚本 vim displayHisoperTime.sh 添加脚本内容如下: if ! grep HISTTIMEFORMAT /etc/bashrc ...

最新文章

  1. 集成服务入门(实验9)日志记录和邮件通知
  2. matlab-绘图-直角坐标系
  3. 如何实现Conditional Include
  4. https HttpsURLConnection请求的单向认证
  5. PHP MySQLi/PDO_MySQL/PDO_SQLite CRUD(增查改删)
  6. 运行scrapy保存图片,报错ValueError: Missing scheme in request url: h
  7. ado filter 多条记录_江苏气动断料锯商家,多条锯_邢台富宇来机械厂
  8. elementui表格宽度适应内容_element ui 表格高度自适应
  9. java循环语句_Java十四天零基础入门-Java for循环语句
  10. 客户端用java api 远程操作HDFS以及远程提交MR任务(源码和异常处理)
  11. android 微信 语音,安卓手机微信不能发语音的解决办法
  12. Power BI 精美的可视化图表
  13. 天眼查python_GitHub - wagaman/Python-Tianyancha: 天眼查爬虫
  14. python中列表的平均值_Python中列表的平均值
  15. 铁矿石加速下跌,沪铜认购大涨,甲醇09-01季节性反套2022.4.22
  16. Open Source
  17. ZOJ4105 Abbreviation
  18. java用代码实现星期菜谱_基于JAVA的菜谱大全接口调用代码实例
  19. 学讲普通话水平测试软件,普通话智能学习软件
  20. 高中选科策略隐私政策

热门文章

  1. AniGAN:可以指定画风生成动漫头像
  2. 动画库-animation.css
  3. python爬虫爬取还未出版的《龙族5悼亡者的归来》小说
  4. foreach遍历ArrayList时的不当操作与解决
  5. c#中显示Excel控件使用说明
  6. csust1086蘑菇真的贵,友情价更高
  7. 微信企业付款到零钱(微信提现)
  8. 1-(3-磺酸基)丙基-1-甲基-2-吡咯烷酮三氟甲磺酸盐[C3SO3Hnmp]CF3SO3
  9. java 导出excel合计_POI导出excel执行自动求和
  10. (转)Confluence 未授权 RCE (CVE-2019-3396) 漏洞分析