当前位置:网站首页>Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
2022-07-23 06:29:00 【Redamancy06】
文章目录
- 1.运算符
- 2.条件判断
- 2.1基本语法
- 2.2常用判断条件
- 2.2.1两个整数之间比较
- 2.2.2按照文件权限进行判断
- 2.2.3按照文件类型进行判断
- 2.2.4测试
- 2.2.4.1test condition
- 2.2.4.2[ condition ](注意 condition 前后要有空格)
- 2.2.4.3在[]里面使用=和!=
- 2.2.4.4-eq 等于(equal) -ne 不等于(not equal)
- 2.2.4.5-lt 小于(less than) -le 小于等于(less equal)
- 2.2.4.6-gt 大于(greater than) -ge 大于等于(greater equal)
- 2.2.4.7判断文件权限、 -r 有读的权限(read)、 -w 有写的权限(write) 、 -x 有执行的权限(execute)
- 2.2.4.8判断文件类型、-e 文件存在(existence)、-f 文件存在并且是一个常规的文件(file)、-d 文件存在并且是一个目录(directory)
- 2.2.4.9多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一 条命令执行失败后,才执行下一条命令)
1.运算符
1.1基本语法
“$((运算式))” 或 “$[运算式]”
1.1.1expr方法
用expr输出+、-、*、%
符号(+、-、*、%)两边需要空格隔开
用*的时候需要用这个\,因为*的用处很多,所以需要区分

1.1.2用expr给a赋值
需要使用命令替换
a=$(运算式)或者用a=`运算式`

1.1.3$((运算式)) 或 $[运算式]

1.1.4用脚本计算两个数的和

在add.sh中写如下代码

2.条件判断
2.1基本语法
1)test condition
2)[ condition ](注意 condition 前后要有空格)
注意:条件非空即为 true,[ atguigu ]返回 true,[ ] 返回 false。
2.2常用判断条件
2.2.1两个整数之间比较
-eq 等于(equal) -ne 不等于(not equal)
-lt 小于(less than) -le 小于等于(less equal)
-gt 大于(greater than) -ge 大于等于(greater equal)
注:如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等。
2.2.2按照文件权限进行判断
-r 有读的权限(read)
-w 有写的权限(write)
-x 有执行的权限(execute)
2.2.3按照文件类型进行判断
-e 文件存在(existence)
-f 文件存在并且是一个常规的文件(file)
-d 文件存在并且是一个目录(directory)
2.2.4测试
2.2.4.1test condition

测试他输出的是正确还是错误使用$?来判断,输出0则表达式为真,输出1表达式为假
2.2.4.2[ condition ](注意 condition 前后要有空格)

用[]的时候,里面的等于号两边一定要用空格空开,否则他会识别为一个整体条件非空即为 true

当[]里面有东西事,则为真,只有空格时为假,如果前后不空格的话就未找到命令
2.2.4.3在[]里面使用=和!=

2.2.4.4-eq 等于(equal) -ne 不等于(not equal)
如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等

2.2.4.5-lt 小于(less than) -le 小于等于(less equal)
不能用< 和<=,因为在linux里表示输入重定向
2.2.4.6-gt 大于(greater than) -ge 大于等于(greater equal)
不能用>和>=,因为在linux里>表示输出重定向
2.2.4.7判断文件权限、 -r 有读的权限(read)、 -w 有写的权限(write) 、 -x 有执行的权限(execute)

2.2.4.8判断文件类型、-e 文件存在(existence)、-f 文件存在并且是一个常规的文件(file)、-d 文件存在并且是一个目录(directory)

2.2.4.9多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一 条命令执行失败后,才执行下一条命令)

边栏推荐
- 2022-07-22 回顾链表操作以及部分问题
- [jzof] path in matrix 12
- [daily training] 814. Binary tree pruning
- Dynamic planning daily practice (1)
- com.mysql.cj.jdbc.exceptions. MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value:
- 倍福PLC和C#通过ADS通信传输int类型变量
- 0722~线程池扩展
- The relationship between method area, perpetual generation and meta space
- com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value:
- Talk about study and work -- Qian Xuesen
猜你喜欢

Beifu PLC and C transmit bool array variables through ads communication

Google面试题原理解析 12个乒乓球其中有1个次品,用天平称重3次找出

使用fastjson解析以及赋予json数据时,json字段顺序不一致问题

Beifu PLC and C # regularly refresh IO through ads communication

MySQL的索引事务&&JDBC编程
![[daily training] 814. Binary tree pruning](/img/c1/d71e6190a1855a392689b55503f75e.png)
[daily training] 814. Binary tree pruning

Machine learning: Li Hang - statistical learning method (II) perceptron + code implementation (primitive + dual form)

Complex networks - common drawing software and libraries

Beifu PLC and C transmit string array type variables through ads communication

倍福PLC--C#ADS通信以通知的方式读取变量
随机推荐
The relationship between method area, perpetual generation and meta space
Opencv image processing (medium) image smoothing + histogram
【MUDUO】Poller抽象类
[noi simulation race] I don't know which CF paper title it is (probability expectation, martingale's stop time theorem)
Deep understanding of the underlying framework of wechat applet (I)
倍福PLC和C#通过ADS通信传输String类型
PHP connecting to SQL Server
Debug No5基础光照
Shooting games lesson 1-2: using sprites
Netease white hat hacker training camp notes (2)
MySQL的索引事务&&JDBC编程
Notes du jour 7
射击 第 1-01 课:入门
倍福PLC和C#通过ADS通信传输Bool数组变量
Charles抓包工具测试实战
Software testing jobs saturated? Automated testing is a new generation of 'offer' skills
Smart city infrastructure management based on bim+3dgis
Problem solving: script file 'scripts\pip script py‘ is not present.
com.mysql.cj.jdbc.exceptions. MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value:
Special topic of MIMO Radar (0) - General Chapter