当前位置:网站首页>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 | how to simulate real use scenarios? Mock technology to help you
- 软件开发中常见模型
- Ubuntu18.04+Centos7配置redis主从【学习笔记】
- 技术分享 | 实战详解接口测试请求方式Get、post
- HDU-5783 Divide the Sequence(贪心水题)
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- 测试面试题集锦(二)| 测试工具篇(附答案)
- Implementation of simple address book in [c language]
- 网络——传输层(详细版)
- How to store floating point data in memory
猜你喜欢

修复故障扇区

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

MySQL master master
![[C language] dynamic memory management](/img/bb/2ec65b38e85f53269dc03d885d70f4.png)
[C language] dynamic memory management

FTP service

What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset

Technology sharing | sending requests using curl
![[C language] string library function introduction and simulation](/img/83/b23fdcbbbd792029083b310c89d4bc.png)
[C language] string library function introduction and simulation

shell脚本——正则表达式

Iptables firewall
随机推荐
raid磁盘阵列
Question brushing record -- binary tree
CentOS7部署MySQL数据库服务器
On cookies and session
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
Network - transport layer (detailed version)
Hdu-1159-commonsubsequence (LCS longest common subsequence)
Technology sharing | sending requests using curl
SSH service configuration
单项链表的创建、遍历以及按要求查找结点
elastic常用高频命令
What kind of air conduction Bluetooth headset with good configuration is recommended
MySQL common commands
Question brushing record - linked list
修复故障扇区
搭建PHP7私有仓库
mongo ssl 配置实战
Ubuntu18.04 set up redis cluster [learning notes]
Installation and configuration of unit test framework jest with typescript
Cocos2d-x learning notes Tile Map tiledmap