条件测试类型
整数测试
字符测试
文件测试
条件测试的表达式
[ 条件表达式 ]
[[ 条件表达式 ]]
test 条件表达式
例子
[root@server ~]# test -e file && echo true || echo false
false
[root@server ~]# touch file
[root@server ~]# test -e file && echo true || echo false
true
[root@server ~]# [ -e file ] && echo true || echo false
true
[root@server ~]# [[ -e file ]] && echo true || echo false
true
[root@server ~]# [[ -e file1 ]] && echo true || echo false
false
整数测试
-eq //测试两个整数是否相等
-ne //测试两个整数是否不等
-gt //测试一个数是否大于另一个数
-lt //测试一个数是否小于另一个数
-ge //大于或等于
-le //小于或等于
示例
#!/bin/bash
a=10
b=8
[ $a -eq $b ]
echo $?
案例
判断是否是两个参数,如果不是两个参数,则显示“必须输入两个参数”,如果输入的是两个参数,则继续运行脚本
判断是否为整数,如果输入的不是整数,则显示“输入的参数必须是整数”,如果输入的整数,则继续执行脚本
比较大小,分为三个阶段:大于、等于、小于
传递参数
[root@server ~]# vim hehe.sh
#!/bin/bash
[ $# -ne 2 ]&&{
echo '必须输入两个参数'
exit
}
expr $1 + $2 >/dev/null 2>&1
[ $? -ne 0 ]&&{
echo '输入的参数必须是整数'
exit
}
[ $1 -gt $2 ]&&{
echo "$1大于$2"
}
[ $1 -eq $2 ]&&{
echo "$1等于$2"
}
[ $1 -lt $2 ]&&{
echo "$1小于$2"
}
[root@server ~]# chmod +x hehe.sh
[root@server ~]# ./hehe.sh
必须输入两个参数
[root@server ~]# ./hehe.sh 1 3 4
必须输入两个参数
[root@server ~]# ./hehe.sh 1 a
输入的参数必须是整数
[root@server ~]# ./hehe.sh 3 3
3等于3
[root@server ~]# .hehe.sh 3 6
3小于6
[root@server ~]# ./hehe.sh3 1
3大于1
0–标准输入 1–标准输出 2—错误输出 >/dev/null 2>&1的作用就是让标准输出重定向到/dev/null中(丢弃标准输出),然后错误输出由于重用了标准输出的描述符,
所以错误输出也被定向到了/dev/null中,错误输出同样也被丢弃了。执行了这条命令之后,该条shell命令将不会输出任何信息到控制台,
也不会有任何信息输出到文件中
通过read
#!/bin/bash
read -p '请输入要比较的两个参数:' n m
expr $n + $m >/dev/null 2>&1
[ $? -ne 0 ]&&{
echo '输入的参数必须是整数'
exit
}
[ $n -gt $m ]&&{
echo "$n大于$m"
}
[ $n -eq $m ]&&{
echo "$n等于$m"
}
[ $n -lt $m ]&&{
echo "$n小于$m"
}
使用if判断
#!/bin/bash
echo "打印菜单,按照选择项选择你喜欢的帅哥"
cat <<EOF
1.pp
2.oo
3.sb
EOF
read -p "please input the num you like: " num
[ $num -eq 1 -o $num -eq 2 -o $num -eq 3 ]&&{
[ $num -eq 1 ]&&{
echo 'I guess you like pp!'
exit 0
}
[ $num -eq 2 ]&&{
echo 'I guess you like oo!'
exit 0
}
[ $num -eq 3 ]&&{
echo 'I guess you like sb!'
exit 0
}
}||{
echo 'I guess you are not human!'
exit 2
}
字符测试
== //等值比较,检查==两边的内容是否一致,==两边都要有空格
!= //检查两边内容是否不一致,不一致为真,一致为假
-z "string" //测试指定字符串是否为空,空则为真,不空则为假
-n "string" //测试指定字符串是否不空,不空则为真,空则为假
错误示范
test "$a"="$b"
#!/bin/bash
a="run"
b="time"
test "$a" = "$b"
echo $?
#!/bin/bash
a="run"
b="time"
test -z "$a"
echo $?
文件测试
存在性测试
-e //测试文件是否存在
存在性及类别测试
-b //测试文件是否为块设备文件
-c //测试文件是否为字符设备文件
-f //测试文件是否为普通文件
-d //测试指定路径是否为目录
-L //测试文件是否为符号链接文件
-p //测试文件是否为命名管道文件
-S //测试文件是否为套接字文件
文件权限测试
-r //测试当前用户对指定文件是否有读权限
-w //测试当前用户对指定文件是否有写权限
-x //测试当前用户对指定文件是否有执行权限
//文件特殊权限测试:
-g //测试文件是否有sgid权限
-u //测试文件是否有suid权限
-k //测试文件是否有sticky权限
文件大小测试
-s //测试文件是否非空
文件是否打开测试
-t fd //fd表示的文件描述符是否已经打开且与某终端相关
双目测试
file1 -ef file2 //测试file1与file2是否指向同一个设备上的相同inode,说白点就是两者是不是同一个文件
file1 -nt file2 //测试file1是否比file2新
file1 -ot file2 //测试file1是否比file2旧
无分类
-N //测试文件自从上一次被读取之后是否被修改过
-O //测试文件是否存在并且被当前用户拥有
-G //测试文件是否存在并且默认组是否为当前用户组
touch /tmp/file1
touch /tmp/file2
mkdir /tmp/dir1
ln -s /tmp/dir1 /tmp/dir2
mkdir /tmp/dir3
chmod g+s /tmp/dir3
vim cc.sh
#!/bin/bash
a=/tmp/file1
b=/tmp/file2
c=/tmp/dir1
d=/tmp/dir2
e=/tmp/dir3
f=/tmp/file4
[ -e $a ]&&echo "file1文件已存在"||echo "file1文件不存在"
[ -e $f ]&&echo "file2文件存在"||echo "file2文件不存在"
[ -f $b ]&&echo "file3这是一个普通文件"||echo "file3不是普通文件"
[ -d $c ]&&echo "dir1这是一个目录"||echo "dir1不是一个目录"
[ -L $d ]&&echo "dir2这是一个链接文件"||echo "dir2不是一个链接文件"
[ -g $e ]&&echo "dir3这个具有SGID权限"||echo "dir3不具有SGID权限"
chmod+x cc.sh
[root@server ~]# ./cc.sh
file1文件已存在
file2文件不存在
file3这是一个普通文件
dir1这是一个目录
dir2这是一个链接文件
dir3这个具有SGID权限
组合测试
-a //与关系 &&
-o //或关系 ||
! //非关系 !=
[ $# -gt 1 -a $# -le 3 ] //两个都是大于1且小于等于3
[ $# -gt 1 ] && [ $# -le 3 ]
[ 1 -eq 1 -a 1 -ne 0 ]
[ 1 -eq 1 ] && [ 1 -ne 0 ]
[ 1 -eq 1 -o 1 -ne 1 ]
[ 1 -eq 1 ] || [ 1 -ne 1 ]
[root@server ~]# [ 1 -eq 0 ] && echo true || echo false
false
[root@server ~]# [ 1 -eq 1 ] && echo true || echo false
true
if语句
单分支if语句
语法:
if 条件;then
执行的命令
fi
案例
判断 当前用户是不是root,如果不是那么返回”ERROR: need to be root
so that!“
[ll@server ~]$ vim ee.sh
#!/bin/bash
if [ $USER != "root" ];then
echo "ERROR: need to be root so that!"
fi
[ll@server ~]$ bash ee.sh
ERROR: need to be root so that!
多分支if
双分支if语法
if 条件;then
执行的命令1
else
执行的命令2
fi
案例
判断当前登录用户是管理员还是普通用户,如果是管理员输出”hey admin“ 如
果是普通用户输出”hey guest“
[ll@server ~]$ cat ee.sh
#!/bin/bash
if [ $USER = "root" ];then
echo "hey admin"
else
echo "hey guest"
fi
[ll@server ~]$ bash ee.sh
hey guest
[root@server ~]# bash /home/haha/ee.sh
hey admin
多分支if语法:
if 条件1;then
执行命令1
elif 条件2;then
执行命令2
elif 条件3;then
执行命令3
else
执行命令3
fi
前面案例使用if判断做
#!/bin/bash
read -p '请输入要比较的两个参数:' n m
expr $n + $m >/dev/null 2>&1
[ $? -ne 0 ]&&{
echo '输入的参数必须是整数'
exit
}
if [ $n -gt $m ];then
echo "$n大于$m"
elif [ $n -eq $m ];then
echo "$n等于$m"
else
echo "$n小于$m"
fi
实例
一、read -p
使用root用户在root用户家目录下新建一个xx.sh的脚本,要求编写脚本判断脚本后输入的进程是否存在
如果进程存在,则输出“the process exists”,如果进程不存在,则输出“the process not exists”。
执行脚本验证systemd和httpd
[root@localhost ~]# cd /home/
[root@localhost home]# vim hehe.sh
#!/bin/bash
read -p '请输入参数:' process
process=`ps -ef | grep $process | grep -v grep | wc -l`
if [ $process -eq 0 ];then
echo 'the process not exists'
else
echo 'the process exists'
fi
[root@localhost home]# ./hehe.sh
请输入参数:systemd
the process exists
[root@localhost home]# ./hehe.sh
请输入参数:httpd
the process exists
[root@localhost home]# systemctl stop httpd
[root@localhost home]# ./hehe.sh
请输入参数:httpd
the process not exists
[root@localhost home]#
二、read -p
1、在/root目录下创建一个文件file1和一个目录dir1
2、在/root目录下创建一个脚本文件xx.sh,要求如下:
(1)输入一个文件名,进行判断该文件的类型
(2)如果未输入任何内容,则返回“错误!输入的文件为空。”
(3)如果输入的文件名不存在,则返回“错误!输入的文件不存在”
(4)如果输入的文件是file1,则返回“file1是一个普通文件”
(5)执行脚本,验证不输出任何数据回车、输入file1、输入dir1、输入file22
[root@localhost ~]# touch file1
[root@localhost ~]# mkdir dir1
[root@localhost ~]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 anaconda-ks.cfg dir1 file1 h.sh initial-setup-ks.cfg
[root@localhost ~]# vim hehe.sh
#!/bin/bash
read -p '请输入文件名:' file
if [ "$file" = "" ];
then
echo "错误!输入的文件为空。"
elif [ ! -e $file ];
then
echo "错误!输入的文件不存在"
elif [ $file = "file1" ];
then
echo "file1是一个普通文件"
elif [ $file != file1 ]&&[ ! -f $file ];
then
echo "$file是其他类型的文件"
fi
[root@localhost ~]# ./hehe.sh
请输入文件名:file1
file1是一个普通文件
[root@localhost ~]# ./hehe.sh
请输入文件名:
错误!输入的文件为空。
[root@localhost ~]# ./hehe.sh
请输入文件名:dir1
dir1是其他类型的文件
[root@localhost ~]# ./hehe.sh
请输入文件名:file22
错误!输入的文件不存在
[root@localhost ~]#
三、read -p
在/root目录下创建一个以xx.sh的脚本,脚本要求如下:
当用户输入的分数大于等于80分且小于等于100分时,才输出“GREAT”字样
当用户输入的分数大于等于60分且小于80分时,才输出“PASS”字样
其他的则输出“FAIL”字样
执行脚本,验证50、70、90分
[root@localhost ~]# vim hehe.sh
#!/bin/bash
read -p "请输入分数:" num
if [ $num -ge 80 -a $num -le 100 ];
then
echo "GREAT"
elif [ $num -ge 60 -a $num -lt 80 ];
then
echo "PASS"
else
echo "FALL"
fi
[root@localhost ~]# ./hehe.sh
请输入分数:50
FALL
[root@localhost ~]# ./hehe.sh
请输入分数:70
PASS
[root@localhost ~]# ./hehe.sh
请输入分数:90
GREAT
[root@localhost ~]#