条件表达式

条件表达式,我们非常的常用,可以说,任何编程语言,都离不开条件表达式,但是每种变成语言的写法都不太一 样,在shell中,有一种独特的写法。

[ 条件 ] ## 不支持 > < 支持:-eq -le -ne
[[ 条件 ]] ## 支持 > < -eq -le -ne
test 条件 ## 都支持 命令行使用test

条件表达式的选项

判断普通文件 -f file

-f:判断文件是否存在
[root@web01 ~]# test -f /tmp/1.txt
[root@web01 ~]# echo $?
1

[root@web01 ~]# test -f /tmp/1.txt && echo '存在'|| echo '不存在'
不存在
[root@web01 tmp]# [ -f /tmp/1.txt ] && echo '存在' || echo '不存在'
不存在
[root@web01 tmp]# [[ -f /tmp/1.txt ]] && echo '存在' || echo '不存在'
不存在

判断目录 -d directory

[root@web01 tmp]# test -d /tmp/a
[root@web01 tmp]# echo $?
0

[root@web01 tmp]# test -d /tmp/a && echo '存在' || echo '不存在'
存在
[root@web01 tmp]# [ -d /tmp/a ] && echo '存在' || echo '不存在'
存在
[root@web01 tmp]# [[ -d /tmp/a ]] && echo '存在' || echo '不存在'
存在

判断文件-e exists

既可以判断文件也可以判断目录
[root@web01 tmp]# test -e /tmp/1.txt && echo '存在' || echo '不存在'
不存在
[root@web01 tmp]# test -e /tmp/a && echo '存在' || echo '不存在'
存在

判断文件有没有读取权限 -r read

[root@web01 tmp]# test -r /tmp/a && echo '可读' || echo '不可读'
可读

判断文件有没有写入权限 -w write

[root@web01 tmp]# test -e /tmp/a && echo '可写' || echo '不可写'
可写

判断文件有没有执行权限 -x execute

[root@web01 tmp]# test -x /tmp/a && echo '可执行' || echo '不可执行'
可执行

判断文件有没有内容-s size

[root@web01 tmp]# test -s /tmp/1.txt && echo '有内容' || echo '文件为空'
文件为空
[root@web01 tmp]# test -s /tmp/a && echo '有内容' || echo '文件为空'
有内容

判断文件是否是一个软链接 -L link

[root@web01 tmp]# test -L /bin && echo '是软链接' || echo '不是软连接'
是软链接
[root@web01 tmp]# test -L /tmp && echo '是软链接' || echo '不是软连接'
不是软连接

对比两个文件的新旧 -nt newer than

[root@web01 tmp]# test /tmp/1.txt -nt /tmp/2.txt && echo '1.txt比2.txt新' || echo '2.txt比1.txt新'
2.txt比1.txt新

[root@web01 tmp]# echo '123' > 1.txt
[root@web01 tmp]# test /tmp/1.txt -nt /tmp/2.txt && echo '1.txt比2.txt新' || echo '2.txt比1.txt新'
1.txt比2.txt新

对比两个文件的新旧 -ot oldder than

[root@web01 tmp]# test /tmp/1.txt -ot /tmp/2.txt && echo '1.txt比2.txt老' || echo '2.txt比1.txt老'
2.txt比1.txt老
[root@web01 tmp]# echo '123' > 2.txt
[root@web01 tmp]# test /tmp/1.txt -ot /tmp/2.txt && echo '1.txt比2.txt老' || echo '2.txt比1.txt老'
1.txt比2.txt老

脚本作业:计算器

1.首先要传递2个参数(数字)

2.传少了报错

报错信息:至少传递两个整数...

3.传的不是数字报错

报错信息:请输入两个整数,不能是字母或特殊符号...

4.计算出传递两个数字的

+

-

*

/

%

#!/bin/bash

# File Name: __calculator.sh__
# Version: __v1.1__
# Author: __yjt__
num1=$1
num2=$2

if [ $# -ne 2 ];then
echo '请输入参数为2个的整数'
else
expr $num1 + $num2 &>/dev/null
[ $? -ne 0 ] && echo '请输入数字...'
expr $num1 + $num2 2>/dev/null
expr $num1 - $num2 2>/dev/null
expr $num1 \* $num2 2>/dev/null
expr $num1 / $num2 2>/dev/null
expr $num1 % $num2 2>/dev/null
fi

字符串表达式

-n:判断字符串是否为空,非空则成立
[root@web01 ~]# abc=''
[root@web01 ~]# test -n "$abc" && echo '字符串不为空' || echo '字符串为空'
字符串为空
### 因为字符串为空,所以不成立,执行了||

-z:判断字符串是否为空,空则成立
[root@web01 ~]# def='123'
[root@web01 ~]# test -z "$def" && echo '字符串为空' || echo '字符串不为空'
字符串不为空
###因为字符串不为空,所以不成立,执行了||

[root@web01 ~]# test -z "$abc" && echo '字符串为空' || echo '字符串不为空'
字符串为空
### 因为字符串为空,所以成立了

'str1' = 'str2':字符串相等则成立
[root@web01 ~]# test 'abc' = 'abc' && echo '字符串相等' || echo '字符串不相等'
字符串相等
### 因为是同一个文件,一定相等,所以成立

[root@web01 ~]# test 'abc' = 'def' && echo '字符串相等' || echo '字符串不相等'
字符串不相等
### 因为不相等,所以没成立

'str1' != 'str2':字符串相等则成立
[root@web01 ~]# test 'abc' != 'def' && echo '字符串不相等' || echo '字符串相等'
字符串不相等
### 因为字符串不相等,所以成立

[root@web01 ~]# test 'abc' != 'abc' && echo '字符串不相等' || echo '字符串相等'
字符串相等
### 因为字符串相等,所以不成立

整数表达式

-eq:等于
-ne:不等于
-lt:小于
-le:小于等于
-gt:大于
-ge:大于等于

[root@web01 ~]# [[ 3\>2 ]] && echo 1 || echo 2
1
## 没有单中括号好用,因为他的大小排列方式是
11
12
13
2
21
3
会导致3大于11
## 不如单中括号[ 3 -gt 2 ]

[[]]正则表达式

=~: 成员运算
[[ =~ ]]

[root@web01 ~]# name=abcdefg
[root@web01 ~]# [[ $name =~ 'a' ]] && echo '包含' || echo '不包含'
包含
[root@web01 ~]# [[ $name =~ ^[a-g]+$ ]] && echo '包含' || echo '不包含'
包含
###贪婪匹配:加了+,表示整段字符
-----------------------------------
[root@web01 ~]# [[ $name =~ ^[a-g]$ ]] && echo '包含' || echo '不包含'
不包含
### 上面的可以理解成以a到g开头结尾,表示单个字符
[root@web01 ~]# [[ $name =~ 'a'$ ]] && echo '包含' || echo '不包含'
不包含
[root@web01 ~]# [[ $name =~ 'g'$ ]] && echo '包含' || echo '不包含'
包含

逻辑表达式

!:非
&&:与 -a [[]] && [[]] [ $num -eq 0 -a $num2 -ne 3 ]
||:或 -o [[]] || [[]] [ $num -eq 0 -o $num2 -ne 3 ]