当前位置:网站首页>shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
2022-07-28 05:28:00 【渔火鳅】
shell脚本——编程条件语句(条件测试、if语句、case分支语句)
一、条件测试
1、test测试
测试表达式是否成立,若成立返回0,否则返回其他数值
注意 空格 空格 空格
格式1:test 条件表达式

2、文件测试
格式
[ 操作符 文件或目录 ]


3、整数测试
整数值比较
[ 整数1 操作符 整数2 ]
| -eq | 等于 equal |
| -ne | 不等于 not equal |
| -gt | 大于 greater than |
| -lt | 小于 lesser than |
| -le | 小于或等于 lesser or equal |
| -ge | 大于或等于 greater or equal |

4、字符串测试
字符串比较
格式1:[ 字符串1 = 字符串2 ]
格式2:[ 字符串1 != 字符串2 ]
格式3:
[ -z 字符串 ] #检查字符串是否为空zero,对于未定义或赋予空值的变量将视为空串
[ -n 字符串 ] #检查是否有字符串存在


(五)逻辑测试

二、if语句
1、if单分支语句



2、if双分支语句

例题

3、if多分支语句

例题

三、case分支语句(类似菜单,有选项用CASE)

例题


四、echo命令-输出字符或提取shell变量的值
1、格式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0lrMXJd2-1657181718542)(C:\Users\YU\AppData\Roaming\Typora\typora-user-images\image-20220706115343958.png)]
2、常用参数

五、for 循环
读取不同的变量值,用来逐个执行同一组命令

例题



例题:输出20以内偶数




注:i++ : i=1+1 先赋值再运算 i=1 之后 再 +1
++i : 1+1=i 先运算再赋值 1+1 之后 再=i
六、while循环
只要条件成立,则反复循环,不成立即停止

1、语法结构
while 表达式
do
command
done
2、例题
打印1-5的数字

注释:死循环,一直输出1

注释:不成立所以输出

7、until循环
重复测试某个条件,只要条件不成立则反复循环

例题

[外链图片转存中…(img-OBiujmZI-1657181718545)]
7、until循环
重复测试某个条件,只要条件不成立则反复循环
[外链图片转存中…(img-QPgIsqpq-1657181718545)]
例题
[外链图片转存中…(img-h6Rtpc5f-1657181718546)]

边栏推荐
猜你喜欢

Technology sharing | common proxy tools for interface testing

SSH服务配置

Skimming records -- sequence traversal of binary tree

Build php7 private warehouse

一、PXE概述和安装

shell脚本——“三剑客”之awk命令

Compilation and preprocessing of C language

Which brand of air conduction earphones is better? These four should not be missed

Dynamic memory management function of C language

yapi漏洞挂马程序chongfu.sh处理
随机推荐
OSI seven layer model
Ubuntu18.04搭建redis集群【学习笔记】
QGraphicsView提升为QChartView
浅谈Cookie和Session
Pku-2739-sum of constructive prime numbers
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
链表中结点的插入和删除
NAT和PAT的原理及配置
Upload and download files from Ubuntu server
Tcp/ip five layer model
技术分享 | 使用postman发送请求
JS four operations are repackaged to solve the problem of precision loss
---Stack & queue---
技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?
Ten thousand words summarize and realize the commonly used sorting and performance comparison
测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
QT uses MSVC compiler to output Chinese garbled code
Prometheus monitoring Nacos
测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?