当前位置:网站首页>Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
2022-07-27 09:49: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多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一 条命令执行失败后,才执行下一条命令)

边栏推荐
- StyleGAN论文笔记+修改代码尝试3D点云生成
- XML概述
- Leetcode.814. binary tree pruning____ DFS
- How does data analysis solve business problems? Here is a super detailed introduction
- 3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
- Nacos configuration center dynamically refreshes the data source
- Why is redis so fast? Redis threading model and redis multithreading
- NFT系统开发-教程
- 拜托!面试请不要再问我 Ribbon 的架构原理
- It's great to write code for 32 inch curved screen display! Send another one!
猜你喜欢

What happens if the MySQL disk is full? I really met you!

Brush the title "sword finger offer" day03

Exercises --- quick arrangement, merging, floating point number dichotomy

Leetcode.814. binary tree pruning____ DFS

会议OA项目之会议排座功能&&会议送审的实现

Meeting seating function of conference OA project & Implementation of meeting submission for approval

超赞的卡尔曼滤波详解文章

Review of in vivo detection

3D face reconstruction and dense alignment with position map progression network

Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
随机推荐
July training (day 24) - segment tree
Summary of binary tree exercises
[scm] source code management - lock of perforce branch
How to install cpolar intranet penetration on raspberry pie
[SCM]源码管理 - perforce 分支的锁定
System parameter constant table of system architecture:
Anchor Free检测器:CenterNet
Review of in vivo detection
注解与反射
学习Typescript(一)
At the end of the year, I'll teach you how to get high performance!
Exercises --- quick arrangement, merging, floating point number dichotomy
Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
I grabbed a ticket and thought I found the system bug of 12306
去 OPPO 面试,被问麻了
I haven't delivered books for a long time, and I feel uncomfortable all over
XML overview
LeetCode.814. 二叉树剪枝____DFS
Engineering survey simulation volume a
吃透Chisel语言.22.Chisel时序电路(二)——Chisel计数器(Counter)详解:计数器、定时器和脉宽调制