条件判断语句
条件判断语法格式
(1)格式1:test 表达式 # man test可以查看很多参数
(2)格式2:[ 条件表达式 ] #[ ]与表达式之间需要空格
(3)[[条件表达式]] 支持正则=
[root@servera ~]# test 1 -eq 2
[root@servera ~]# echo $? #如果是0,代表成立,不是,不成立
1
[root@servera ~]# test 1 -eq 1
[root@servera ~]# echo $?
0
[root@servera ~]# [ 1 -eq 2 ];echo $?
1
[root@servera ~]# [ 1 -eq 1 ];echo $?
0
判断什么
1.文件类型(是否存在,是不是普通文件套接子…)
2.文件权限
3.文件新旧
4.字符串是否相等
1.文件类型(是否存在,是不是普通文件套接子…)
-e FILE #判断文件是否存在(任何类型文件)
FILE exists
-f FILE #判断文件是否存在并且是一个普通文件
FILE exists and is a regular file
-d FILE #判断文件是否存在并且是一个目录
FILE exists and is a directory
-L FILE #判断文件是否存在并且是一个软链接
FILE exists and is a symbolic link (same as -h)
-p FILE #判断文件是否存在并且是一个命名管道文件
FILE exists and is a named pipe
-b FILE #判断文件是否存在并且是一个块设备文件
FILE exists and is block special
-c FILE #判断文件是否存在并且是一个字符文件
FILE exists and is character special
-s FILE #判断文件是否存在并且是一个非空文件
FILE exists and has a size greater than zero
-S FILE #判断文件是否存在并且是一个套接字文件
FILE exists and is a socket
测试:
[root@servera ~]# test -d /mnt
[root@servera ~]# echo $?
0
[root@servera ~]# test -d /etc/passwd
[root@servera ~]# echo $?
1
[root@servera ~]# test -e /mnt
[root@servera ~]# echo $?
0
[root@servera ~]# test -e kaixin
[root@servera ~]# echo $?
1
[root@servera ~]# test -f /etc/passwd
[root@servera ~]# echo $?
0
[root@servera ~]# cd /root/Desktop/
[root@servera Desktop]# mkdir test
[root@servera Desktop]# cd test/
[root@servera test]# touch file{1..3}
[root@servera test]# ln -s file1 file4
[root@servera test]# ll
total 0
-rw-r--r--. 1 root root 0 Jul 10 21:59 file1
-rw-r--r--. 1 root root 0 Jul 10 21:59 file2
-rw-r--r--. 1 root root 0 Jul 10 21:59 file3
lrwxrwxrwx. 1 root root 5 Jul 10 22:00 file4 -> file1
[root@servera test]# test -L file4
[root@servera test]# echo $?
0
[root@servera test]# test -L file3
[root@servera test]# echo $?
1
[root@servera test]# rm -fr file1
[root@servera test]# ll
total 0
-rw-r--r--. 1 root root 0 Jul 10 21:59 file2
-rw-r--r--. 1 root root 0 Jul 10 21:59 file3
lrwxrwxrwx. 1 root root 5 Jul 10 22:00 file4 -> file1
[root@servera test]# test -L file4
[root@servera test]# echo $?
0
[root@servera test]# test -e file4
[root@servera test]# echo $?
1
# 判断软连接时,如果连接的文件不存在,-L并不能检测出来。
解决方案:先用-e判断连接的文件是否有效(存在?),然后判断是否是一个软连接
2.文件权限
-r FILE #当前用户对其是否有读权限
FILE exists and read permission is granted
-x FILE #当前用户对其是否有可执行权限
FILE exists and execute (or search) permission is granted
-w FILE #当前用户对其是否有写权限
FILE exists and write permission is granted
-u FILE #当前用户对其是否有suid(u+s)特殊权限(高级权限冒险位)
FILE exists and its set-user-ID bit is set
-g FILE #当前用户对其是否有guid特殊权限(高级权限强制位)
FILE exists and is set-group-ID
-k FILE #当前用户对其是否有o+t特殊权限(高级权限粘滞位)
FILE exists and has its sticky bit set
测试:
[root@servera ~]# ll -d /tmp #/tmp 是里linux系统中使用o+t权限的目录
#o+t权限是只有root和文件所有者可以删除,其他用户不能删除,只能读写
drwxrwxrwt. 15 root root 4096 Jul 10 21:59 /tmp
[root@servera ~]# ll /usr/bin/passwd #/usr/bin/passwd是linux系统中u+s权限的文件
#u+s只有root可以更改,其他用户只能读
-rwsr-xr-x. 1 root root 34512 Aug 12 2018 /usr/bin/passwd
[kiosk@foundation45 Desktop]$ [ -r /etc/passwd ];echo $?
0
#判断root用户对/etc/passwd是否有读(read)权限,也可以测试w,x
#也可以测试普通用户对该文件的权限
[kiosk@foundation45 Desktop]$ [ -w /etc/passwd ];echo $?
1
[kiosk@foundation45 Desktop]$ [ -x /etc/passwd ];echo $?
1
[root@servera ~]# ll /etc/shadow #用户密码存在的地方
----------. 1 root root 1311 May 21 2019 /etc/shadow
4.判断root用户是否对/usr/bin/passwd有u+s权限
[root@servera ~]# [ -u /usr/bin/passwd ];echo $?
0
5.判断root用户是否对/tmp有o+t权限
[root@servera ~]# [ -k /usr/bin/passwd ];echo $?
1
[root@servera ~]# [ -k /home ];echo $?
1
3.判断文件新旧
FILE1 -nt FILE2 #比较file1是否比file2新
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2 #比较file1是否比file2旧
FILE1 is older than FILE2
FILE1 -ef FILE2 #equal比较是否为同一个文件,或者用于判断硬连接,是否指向同一个inode
FILE1 and FILE2 have the same device and inode numbers
测试:
[root@servera test]# ll
total 0
-rw-r--r--. 1 root root 0 Jul 10 21:59 file2
-rw-r--r--. 1 root root 0 Jul 10 21:59 file3
lrwxrwxrwx. 1 root root 5 Jul 10 22:00 file4 -> file1
[root@servera test]# vim file2
[root@servera test]# ll
total 4
-rw-r--r--. 1 root root 14 Jul 10 23:53 file2 #编辑之后时间改变
-rw-r--r--. 1 root root 0 Jul 10 21:59 file3
lrwxrwxrwx. 1 root root 5 Jul 10 22:00 file4 -> file1
[root@servera test]# [ file2 -nt file3 ];echo $? # file3比file2更新吗
0 #是的
[root@servera test]# [ file2 -ot file4 ];echo $? # file2比file4更旧吗
1 #错的
条件判断相关整数
INTEGER1 -eq INTEGER2 #相等
INTEGER1 is equal to INTEGER2
INTEGER1 -ne INTEGER2 #不相等
INTEGER1 is not equal to INTEGER2
INTEGER1 -gt INTEGER2 #大于
INTEGER1 is greater than INTEGER2
INTEGER1 -ge INTEGER2 #大于等于
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -lt INTEGER2 #小于
INTEGER1 is less than INTEGER2
INTEGER1 -le INTEGER2 #小于等于
INTEGER1 is less than or equal to INTEGER2
测试用户是不是root用户
[root@servera test]# [ `id -u` -eq 0 ] && echo user is root || echo not root
user is root
# 命令 id -u 测试用户的uid,root的uid为0
[kiosk@foundation45 Desktop]$ [ `id -u` -eq 0 ] && echo user is root || echo not root
not root
# 反向单引``表示获取命令的执行结果
4.判断字符串
-n STRING #判断是否为非空字符串,字符串长度大于0
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING #判断是否为空字符串,字符串长度为0
the length of STRING is zero
STRING1 = STRING2 #判断字符串是否相等
the strings are equal
STRING1 != STRING2 #判断字符串是否不相等
the strings are not equal
测试:
[root@servera ~]# [ -z '' ];echo $?
0
[root@servera ~]# [ -n '' ];echo $?
1
[root@servera ~]# [ -z 'wef' ];echo $?
1
[root@servera ~]# [ -n 'wef' ];echo $?
0
[root@servera ~]# [ 'root' = 'kiosk' ];echo $?
1
[root@servera ~]# [ 'root' = 'root' ];echo $?
0
[root@servera ~]# [ 'root' == 'kiosk' ];echo $?
1
[root@servera ~]# [ 'root' != 'kiosk' ];echo $?
0
字符串判断的参数:
重点:shell编程中,判断字符串是否相等,=和==均可
多重条件判断
-a 和 && 逻辑与,全真则真
[ -f /etc/passwd -a -f /etc/group ]
[ -f /etc/passwd ] && [ -f /etc/group ]
-o 和 || 逻辑或,全假则假
[ -f /etc/passwd -o -f /etc/group ]
[ -f /etc/passwd ] || [ -f /etc/group ]
1.&&如果前面条件成立,执行什么事情
2.||如果前面不成立,执行什么事情
|| #如果前面为真,后面就不再判断
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
测试:
判断/etc/passwd是否存在并且当前用户对它是否有读写权限
[root@servera ~]# [ -e /etc/passwd ]&&[ -r /etc/passwd ]&&[ -w /etc/passwd ];echo $?
0
[root@servera ~]# [ -e /etc/passwd -a -r /etc/passwd -a -w /etc/passwd ];echo $?
0
[root@servera ~]# ll /etc/passwd
-rw-r--r--. 1 root root 2495 May 21 2019 /etc/passwd
[root@servera ~]# [ -e /etc/passwd ]||[ -r /etc/passwd ]||[ -w /etc/passwd ];echo $?
0
[root@servera ~]# [ -e /etc/passwd -o -r /etc/passwd -o -w /etc/passwd ];echo $?
0
流程控制语句(if…else)
格式1:
if 条件判断语句;then
command 1
command 2
fi
格式2:
if 条件判断语句;then
command 1
command 2
else
fi
格式3:
if 条件判断语句;then
command 1
elif 条件判断语句;then
command 2
else
command 3
fi
测试1:
1.根据当前登录用户uid判断是否为超级用户?
提示:uid=0代表超级用户
[root@servera mnt]# vim test1.sh
#!/bin/bash
if [ `id -u` -ne 0 ];then
echo "the user is not root"
else
echo "the user is root"
fi
[root@servera mnt]# sh test1.sh
the user is root
测试2:
判断当前主机是否和远程主机ping通
#!/bin/bash
ping -c1 $1 &>/dev/null
if [ $? = 0 ] # $?是返回值,echo $?只是显示了一下,并没有值
then
echo "$1 is exist!"
else
echo "$1 is not exist!"
fi
$变量名 #获取变量里面的值
pgrep命令
以名称为依据从运行进程队列中查找进程,并显示查找到的进程id
-o :仅显示找到的最小(起始)进程号
-n : 仅显示找到的最大(结束)进程号
-l : 显示进程名称
-P : 指定父进程号;pgrep -p 4764 查看父进程下的子进程id
-g : 指定进程组
-t : 指定开启进程的终端
-u : 指定进程的有效用户ID
开启火狐
[kiosk@foundation45 mnt]$ ps aux|grep firefox #显示进程
关闭火狐
[kiosk@foundation45 mnt]$ ps aux|grep firefox #依然显示进程,但是火狐已经关了
[kiosk@foundation45 mnt]$ echo $?
0
[kiosk@foundation45 mnt]$ pgrep firefox
[kiosk@foundation45 mnt]$ echo $?
1
[kiosk@foundation45 mnt]$ firefox &
[1] 9516
[kiosk@foundation45 mnt]$ pgrep firefox
9516
循环判断语句(for,while,until)
for 循环语句
for…do…done
- 列表for循环:用于将一组命令执行已知的次数
- 不带列表的for循环执行时由用户指定参数和参数个数
- 类C风格的for循环
1.列表循环
for var in {1..10};do echo $var;done
for var in {0..10..2};do echo $var;done #从0循环到10,间隔为2(0,2,4,6,8,10)
for var in {10..1..2};do echo $var;done #10,8,6,4,2
for var in 1 2 3 4 5;do echo $var;done
for var in `seq 10`;do echo $var;done #从1到10
for var in `seq 10 -2 1`;do echo $var;done #从10到1,每次减2(10,8,6,5,4,2)
for var in $(seq 10);do echo $var;done
2.不带列表循环
for var
do
echo $var
done
echo "脚本后面有$#个参数"
3.类C语言
for ((i=1;i<=5;i++))
do
echo $i
done