加载中...
Linux Shell条件测试
发表于:2021-09-14 | 分类: 平台

2 Shell条件测试

2.1 文件测试

  • 在Shell编程中,通常使用 test 命令进行条件测试
  • 语法形式为“test <测试表达式>”。
1
2
3
4
5
6
[root@hdp-1 ~]# test -d /home
[root@hdp-1 ~]# echo $?
0
[root@hdp-1 ~]# test -d /homo
[root@hdp-1 ~]# echo $?
1
  • test 条件测试在脚本中的应用
1
2
3
4
5
6
7
8
9
10
11
[root@hdp-1 shell]# cat test_mysqlback.sh 
#!/usr/bin/bash

back_dir=~/mysql_back
if ! test -d $back_dir;then
mkdir -p $back_dir
fi

echo "开始备份..."
[root@hdp-1 shell]# chmod +x test_mysqlback.sh
[root@hdp-1 shell]# bash -vx test_mysqlback.sh
  • 除 test 外,还可以使用中括号或双中括号进行条件测试
  • 中括号 [ 是Shell的内置命令,不是标点符号
1
2
3
4
5
6
7
8
9
10
11
[root@hdp-1 shell]# [ -d /home ]
[root@hdp-1 shell]# echo $?
0
[root@hdp-1 shell]# [ -d /homo ]
[root@hdp-1 shell]# echo $?
1
[root@hdp-1 shell]# type -a [
[ 是 shell 内嵌
[ 是 /usr/bin/[
[root@hdp-1 shell]# [[ -d /home ]];echo $?
0
  • 文件测试操作符
操作 描述
-d 测试是否为目录(Directory)
-a 测试目录或文件是否存在(Exist)
-f 测试是否为文件(File)
-r 测试当前用户是否可读(read)
-w 测试当前用户是否可写(write)
-x 测试当前用户是否可执行(cxcutc)
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@hdp-1 shell]# ll host.txt 
-rw-r--r--. 1 root root 102 11月 2 15:41 host.txt
[root@hdp-1 shell]# [ -r host.txt ];echo $?
0
[root@hdp-1 shell]# chmod -r host.txt
[root@hdp-1 shell]# ll host.txt
--w-------. 1 root root 102 11月 2 15:41 host.txt
[root@hdp-1 shell]# [ -r host.txt ];echo $?
0
[root@hdp-1 shell]# [ -w host.txt ];echo $?
0
[root@hdp-1 shell]# [ -x host.txt ];echo $?
1

2.2 整数测试

  • 整数测试通常用于数值之间的运算,其语法格式为 [ 整数1 操作符 整数2 ]
  • 或 test 整数1 操作符 整数2。
操作符 含义
-eq 等于(Equal)
-ne 不等于(Not Equal)
-gt 大于(Greater Than)
-lt 小于(Lesser Than)
-le 小于或等于(Lesser or Equal)
-ge 大于或等于(Greater or Equal)
  • 整数测试在脚本中的应用:创建新用户
1
2
3
4
5
6
7
8
9
10
11
12
[root@hdp-1 shell]# cat create_user01.sh 
#!/bin/bash
read -p "Input a username: " user
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exsits."
else
useradd $user
if [ $? -eq 0 ];then
echo "$user created."
fi
fi
  • 整数测试在脚本中的应用:磁盘可用空间告警
1
2
3
4
5
6
7
8
9
[root@hdp-1 shell]# cat disk_use.sh 
#!/bin/bash
. /etc/profile
disk_use=`df -Th |grep '/$'|awk '{print $(NF-1)}'|awk -F"%" '{print $1}'`
if [ $disk_use -ge 5 ];then
echo "`date +%F" "%T` disk used: ${disk_use}%"
fi
[root@hdp-1 shell]# ./disk_use.sh
2021-12-06 14:53:46 disk used: 42%
  • 整数测试在脚本中的应用:内存可用空间告警
  • shell 语法检查:bash -n xxx.sh
1
2
3
4
5
6
7
8
9
10
11
12
[root@hdp-1 shell]# cat mem_use.sh 
#!/bin/bash
mem_used=`free -m |grep '^M' |awk '{print $3}'`
mem_total=`free -m |grep '^M' |awk '{print $2}'`
mem_percent=$[mem_used*100/mem_total]
var_file=`date +%F`.log
if [ $mem_percent -ge 10 ];then
echo "`date +%F" "%T` memory used: ${mem_percent}%" >$var_file
fi
[root@hdp-1 shell]# ./mem_use.sh
[root@hdp-1 shell]# cat 2021-12-06.log
2021-12-06 14:54:44 memory used: 79%

2.3 字符串测试

  • 字符串测试操作符的作用包括比较字符串是否相同、测试字符串的长度是否为0。书写表达式为[ 字符串1 = 字符串2 ]、[ 字符串1 != 字符串2 ]或[ -z 字符串 ]。字符串测试运算符如表所示。
符号 含义
-z 判断字符串长度是否为0
-n 判断字符串长度是否为非0
! 判断两个字符串是否不相等
= 判断两个字符串是否相等
1
2
3
4
5
6
[root@hdp-1 shell]# [ $USER = root ];echo $?
0
[root@hdp-1 shell]# [ $USER = hadoop ];echo $?
1
[root@hdp-1 shell]# [ $USER != hadoop ];echo $?
0
  • 双引号的作用
1
2
3
4
5
6
7
8
9
10
11
[root@hdp-1 shell]# [ "$USER" != hadoop ];echo $?
0
[root@hdp-1 shell]# [ "$USER" = root ];echo $?
0
[root@hdp-1 shell]# echo $username

[root@hdp-1 shell]# [ $username = root ];echo $?
-bash: [: =: 期待一元表达式
2
[root@hdp-1 shell]# [ "$username" = root ];echo $?
1
  • 字符串长度测试:空串、未定义变量的长度都是 0
  • 字符串必须使用双引号
1
2
3
4
5
6
7
8
9
10
11
[root@hdp-1 shell]# var=""
[root@hdp-1 shell]# echo ${#var}
0
[root@hdp-1 shell]# [ -z $var ];echo $?
0
[root@hdp-1 shell]# [ -n $var ];echo $?
0
[root@hdp-1 shell]# [ -z "$var" ];echo $?
0
[root@hdp-1 shell]# [ -n "$var" ];echo $?
1
  • 字符串测试在脚本中的应用
1
2
3
4
5
6
7
8
[root@hdp-1 shell]# cat install.sh 
#!/bin/bash
if [ $user ! = root ];then
echo "Permission Denied"
exit
fi

yum install httpd
  • 以上是安装服务的脚本,判断变量user的值是否为root,如果为root则安装httpd,
    如果不是root,则显示”你没有权限”。

2.4 逻辑运算符

  • 在Shell条件测试中,使用逻辑运算符实现复杂的条件测试,逻辑运算符用于操作两个变量。逻辑运算符语法格式。
1
2
3
[ 表达式1 ] 操作符 [ 表达式2 ]
or
命令1 操作符 命令2
  • 逻辑操作符
运算符 含义
-a 或 && 判断操作符两边均为真,结果为真,否则为假,”逻辑与”
-o 或 || 判断操作符两边一边为真,结果为真,否则为假,”逻辑或”
! 判断操作符两边均为假,结果为真,否则为假,”逻辑否”
  • -a和&&的运算规则。
1
2
3
4
5
6
7
8
9
[root@hdp-1 shell]# [ 1 -lt 2 -a 5 -gt 10 ];echo $?
1
[root@hdp-1 shell]# [ 1 -lt 2 -o 5 -gt 10 ];echo $?
0
[root@hdp-1 shell]# [[ 1 -lt 2 && 5 -gt 10 ]];echo $?
1
[root@hdp-1 shell]# [[ 1 -lt 2 || 5 -gt 10 ]];echo $?
0
[root@hdp-1 shell]#
  • 应用:创建批量用户
1
2
3
4
5
6
7
8
9
10
11
12
[root@hdp-1 shell]# cat user_add.sh 
#!/bin/bash
read -p "Please input a username: " user
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exists."
else
useradd $user
if [ $? -eq 0 ];then
echo "user created."
fi
fi
  • 应用:创建批量用户

case 条件语句

  • case条件语句相当于多分支的if/elif/else条件语句
  • case条件语句比if语句更加简洁工整,故常应用在实现系统服务启动脚本等应用场景中。
  • 在Shell编程中,case语句有固定的语法格式。其语法格式为:
1
2
3
4
5
6
7
8
9
10
11
12
13
case 变量值 in
条件表达式1)
代码块1
;;
条件表达式2)
代码块2
;;
条件表达式3)
代码块3
;;
*)
默认代码块
esac
  • 条件表达式匹配如表所示。
条件表达式 说明
* 任意字符
? 任意单个字符
[abc] 其中之一
[a-z] 区间之一
| 多重选择
  • case条件语句案例实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@hdp-1 shell]# cat user_del.sh 
#!/bin/bash
# delete user

read -p "Input a username: " user
id $user &>/dev/null
if [ $? -ne 0 ];then
echo "no such user: $user"
exit 1
fi
read -p "Are you sure?[y/n]: " action
case "$action" in
y|Y|yes|YES)
userdel -r $user
echo "user $user deleted. "
;;
*)
echo "error! "
exit 2
esac

上一篇:
Linux Shell循环结构
下一篇:
Linux Shell进阶
本文目录
本文目录