shell中的运算(数学比较,文件检查与比较、字符串比较、赋值运算、逻辑运算)
一,数学比较运算
1,数学比较运算(整型)
-eq 等于
-gt 大于
-lt 小于
-ge 大于或等于
-le 小于或等于
-ne 不等于
[root@CentOs shell]# test 1 -eq 1;echo $?
0(相等返回0)
[root@CentOs shell]# test 1 -gt 1;echo $?
1(不相等返回1)
[root@CentOs shell]#
遇到小数怎么处理?
转换为整数比较!
[root@CentOs shell]# echo "1.5*10"|bc
15.0
[root@CentOs shell]# echo "1.5*10"|bc|cut -d '.' -f1(以“.”为分隔符,打印点的前一位)
15
[root@CentOs shell]# test `echo "1.5*10"|bc|cut -d '.' -f1` -eq $((2*10));echo $?
1
[root@CentOs shell]#
或者放在脚本中执行
[root@CentOs shell]# vim ShellTest
#!/bin/bash
NUM1=`echo "1.5*10"|bc|cut -d '.' -f1`
NUM2=$((2*10))
test $NUM1 -eq $NUM2;echo $?
[root@CentOs shell]# bash ShellTest
1
[root@CentOs shell]# bash -x ShellTest (-x相当于debug)
++ echo '1.5*10'
++ bc
++ cut -d . -f1
+ NUM1=15
+ NUM2=20
+ test 15 -eq 20
+ echo 1
1
[root@CentOs shell]#
二、文件比较与检查:
(1)-d:检查文件是否存在且为目录
[root@CentOs shell]# test -d /opt/shell/abc;echo $?
1
[root@CentOs shell]#
(2)-e:检查文件或者目录是否存在
[root@CentOs shell]# mkdir /opt/shell/abc
[root@CentOs shell]# test -e /opt/shell/abc;echo $?
0
[root@CentOs shell]#
(3)-f:检查文件是否存在且为文件
[root@CentOs shell]# test -f /opt/shell/abc;echo $?
1
[root@CentOs shell]#
(4)-r:检查文件是否存在且为可读
[root@CentOs shell]# test -r /opt/shell/abc;echo $?
0
[root@CentOs shell]# ls -alh
总用量 16K
drwxr-xr-x. 3 root root 86 3月 31 11:27 .
drwxr-xr-x. 5 root root 53 3月 26 18:13 ..
drwxr-xr-x. 2 root root 6 3月 31 11:27 abc
-rw-r--r--. 1 root root 34 3月 26 21:30 exit_code.sh
-rw-r--r--. 1 root root 19 3月 26 17:56 hello.sh
-rw-r--r--. 1 root root 96 3月 30 12:02 ShellTest.sh
-rw-r--r--. 1 root root 131 3月 31 10:56 wz.sh
[root@CentOs shell]#
(5)-s:检查文件是否存在且不为空
[root@CentOs shell]# test -s /opt/shell/abc;echo $?
0
[root@CentOs shell]#
(6)-w:检查文件是否存在且可写
[root@CentOs shell]# test -w /opt/shell/abc;echo $?
0
[root@CentOs shell]#
切换用户:
[slime@CentOs shell]$ test -w /opt/shell/abc;echo $?
1
[slime@CentOs shell]$ ls -alh
总用量 16K
drwxr-xr-x. 3 root root 86 3月 31 11:27 .
drwxr-xr-x. 5 root root 53 3月 26 18:13 ..
drwxr-xr-x. 2 root root 6 3月 31 11:27 abc
-rw-r--r--. 1 root root 34 3月 26 21:30 exit_code.sh
-rw-r--r--. 1 root root 19 3月 26 17:56 hello.sh
-rw-r--r--. 1 root root 96 3月 30 12:02 ShellTest.sh
-rw-r--r--. 1 root root 131 3月 31 10:56 wz.sh
[slime@CentOs shell]$
(7)-x:检查文件是否存在且可执行
[root@CentOs shell]# test -x /opt/shell/abc;echo $?
0
[root@CentOs shell]#
切换用户:
[slime@CentOs shell]$ ls -alh
总用量 16K
drwxr-xr-x. 3 root root 86 3月 31 11:27 .
drwxr-xr-x. 5 root root 53 3月 26 18:13 ..
drwxr-xr-x. 2 root root 6 3月 31 11:27 abc
-rw-r--r--. 1 root root 34 3月 26 21:30 exit_code.sh
-rw-r--r--. 1 root root 19 3月 26 17:56 hello.sh
-rw-r--r--. 1 root root 96 3月 30 12:02 ShellTest.sh
-rw-r--r--. 1 root root 131 3月 31 10:56 wz.sh
[slime@CentOs shell]$ test -x /opt/shell/abc;echo $?
0
[slime@CentOs shell]$
(8)-O:检查文件是否存在且被当前用户拥有
[slime@CentOs shell]$ test -O /opt/shell/abc;echo $?
1
[slime@CentOs shell]$
(9)-G:检查文件是否存在且默认组为当前用户组
[slime@CentOs shell]$ test -G /opt/shell/abc;echo $?
1
[slime@CentOs shell]$
flie1 -nt flie2 :检查file1是否比file2新
flie1 -ot flie2 :检查file1是否比file2旧
(比较的是文件的修改时间)
三、字符串比较运算
运算符解释,注意字符串别忘了使用引号引起来
== 等于
[root@CentOs shell]# test $USER == 'root';echo $?
0
[root@CentOs shell]#
与赋值运算区分开!
!= 不等于
[root@CentOs shell]# test $USER != 'root1';echo $?
0
[root@CentOs shell]#
-n 检查字符串的长度是否大于0
[root@CentOs shell]# test -n "abc";echo $?
0
[root@CentOs shell]#
-z 检查字符串的长度是否为0
[root@CentOs shell]# test -z "";echo $?
0
[root@CentOs shell]#
四、赋值运算
= 赋值运算符
如:a=18;name="heitui"
五、逻辑运算
&& 与运算
|| 或运算
! 非运算
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律