当前位置:网站首页>shell运算符-数学运算,关系运算,字符串运算,文件检测运算
shell运算符-数学运算,关系运算,字符串运算,文件检测运算
2022-07-21 11:26:00 【悟红尘】
Shell中是不支持简单数学运算的,比如这样:
[root@VM_0_11_centos ~]# rest=10+10
[root@VM_0_11_centos ~]# echo $rest
10+10
但是我们可以加上命令使其支持,在shell中有三种弄方法可以支持简单的数学运算:
- 使用$(())
这个$后面是两个小括号,一个小括号不对:
[root@VM_0_11_centos ~]# rest=$((10+20))
[root@VM_0_11_centos ~]# echo $rest
30
[root@VM_0_11_centos ~]# rest=$(10+20)
-bash: 10+20: command not found
- 使用$[]
使用$[]这个感觉是最简单的
[root@VM_0_11_centos ~]# rest=$[10 + 10]
[root@VM_0_11_centos ~]# echo $rest
20
3.使用关键字expr
使用这个expr有点复杂,在使用的时候需要用反单引号将其包裹起来,而且必须有空格:
[root@VM_0_11_centos ~]# rest=`expr 10 + 20`
[root@VM_0_11_centos ~]# echo $rest
30
如果没有空格的话:
[root@VM_0_11_centos ~]# rest=`expr 10+20`
[root@VM_0_11_centos ~]# $rest
-bash: 10+20: command not found
而且还不支持括号,并且乘号还要用反斜杠给转译一下:
[root@VM_0_11_centos ~]# rest=`expr 10 + 20 \* (10 + 10)`
-bash: command substitution: line 1: syntax error near unexpected token `('
-bash: command substitution: line 1: `expr 10 + 20 \* (10 + 10)'
[root@VM_0_11_centos ~]#
所以遇到复杂的运算,需要一步一步的算,很麻烦!
还有一些其他的简单运算 比如乘除,取余等都类似
关系运算:
一般用条件语句上,需要用到[]和(())
比如 大于 在[]中要用-gt表示 ,在(())用==表示,gt是 greater than的缩写
例如 在文件中写入:
if (($1 > $2))
then
echo "$1 大于 $2"
else
echo "$1 小于 $2"
fi
if [ $1 -gt $2 ]
then
echo "$1 大于 $2"
else
echo "$1 小于 $2"
fi
执行:
[root@VM_0_11_centos shell]# ./myShell.sh 100 200
100 小于 200
100 小于 200
shell的if语句也挺奇怪,if 之后是换行,然后then 最后需要加上fi,表示结束。 如果多个判断的话用elif,不是else if,跟java还是有区别的
需要注意的一点的是[]中括号使用的时候,一定要主要前后的空格,否则会报错的。
其他的关系运算类似:
-eq 表示相等,在(())用 ==表示,是equal的缩写
-ne表示不相等,在(())用 !=表示,是 not equal的缩写
-gt 表示大于,在(())用 >表示,是greater than的缩写
-ge 表示大于等于,在(())用 >=表示,是greater equall的缩写
-lt 表示小于,在(())用 <表示,是litter than的缩写
-le 表示小于等于,在(())用 <=表示,是litter equal的缩写
逻辑运算符
逻辑运算符,一般是指与或非
在shell []中 -a表示与,就是两个表达式都成立,条件才成立,在(())表示为&&
-o或运算,表示两个条件只要有一个为真即可,在(()))表示为||
!表示非 在(()))表示为!
字符串运算符
= 检测两个字符串是否相等,相等则返回true
!= 检测两个字符串是否相等,不相等则返回true
-z 检测字符串长度是否为0,为0则返回true
-n 检测字符串长度是否为0,不为0则返回true
str 检测字符串是否为null,不为null则返回true
比如:
str="abcdee"
if [ $str = "abc" ]
then
echo "相等"
else
echo "不相等"
fi
if [ -n $str ]
then
echo "不为0"
else
echo "为0"
fi
执行结果:
[root@VM_0_11_centos shell]# ./myShell.sh
不相等
不为0
文件检测运算符:
-b 检测文件是否是块设备文件,如果是,则返回true
-c 检测文件是否是字符设备文件,如果是,则返回true
-d 检测文件是否是目录文件,如果是,则返回true
-f 检测文件是否是普通文件(既不是目录也不是设备文件),如果是,则返回true
-g 检测文件是否设置了SGID位,如果是,则返回true
-k 检测文件是否设置了粘着位(stucky Bit),如果是,则返回true
-p 检测文件是否具名管道,如果是,则返回true
-u 检测文件是否设置了SUID位,如果是,则返回true
-r 检测文件是否可读,如果是,则返回true
-w 检测文件是否可写,如果是,则返回true
-x 检测文件是否可执行,如果是,则返回true
-s 检测文件是否为不为空(文件大小是否不为0),如果不为0,则返回true
-e 检测文件(包括目录)是否存在,如果存在,则返回true
-a 检测文件(包括目录)是否存在,如果存在,则返回true
比如检测文件是否存在用-e 比如创建一个文件test.txt
file=/root/training/shell/test.txt
if [ -e $file ]
then
echo "文件存在"
else
echo "文件不存在"
fi
执行的结果:
[root@VM_0_11_centos shell]# ./myShell.sh
文件存在
欢迎关注我的公众号:北风中独行的蜗牛
参考文章:
https://blog.csdn.net/yuki5233/article/details/81166509
https://www.runoob.com/linux/linux-shell-basic-operators.html
边栏推荐
- 从0开始实现一个代理池
- After reading "what to read in college?"
- 执掌英国工程技术学会13载,范纳杰正式退休
- 进程池及回掉函数[通俗易懂]
- Seckill design
- When servlet writes webapp, filter interception is used to realize login verification
- asp.net core、c#关于路径的总结
- Field injection is not recommended
- Compilation Principle Experiment 1 -- principle and implementation of lexical analysis program design
- 国产数据库的春天里
猜你喜欢
ANSVC无功补偿装置助力江苏某环保能源项目
预付费平台关于电改政策的设计与应用
Monai label installation process and use strategy
How to build a good knowledge base management system?
50个名额限量开放|带着OceanBase年度发布会的消息走来了!
Idea建文件夾時,文件夾的空文件夾的展開與重疊
Web
Ansvc reactive power compensation device helps an environmental protection energy project in Jiangsu
Application of DC distribution system products at the end of data center
Field injection is not recommended怎么处理
随机推荐
日期工具类
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
2022年7月31日DAMA-CDGA/CDGP数据治理认证班开启!
3.3栈和队列的应用
ANSVC无功补偿装置助力江苏某环保能源项目
湘潭市党政代表团莅临麒麟信安调研考察
C language exercises - structure and union
A trick to teach you how to master online videos
asp.net coree文件上传与下载实例
2.3线性表的链式表示(2)
5.1身份认证
关于Thread.sleep()方法
Date tool class
定时任务框架
The use of anonymous inner classes in development
Cadence OrCAD Capture TCL/TK脚本实例
栈的应用
从0开始实现一个代理池
Monai label installation process and use strategy
PAM4科普