1、文件测试vim
文件测试大多都是单目测试,其用法相对简单,其格式为:bash
操做符:文件路径ssh
能够用来测试的选项有:ide
-f:测试其是否为普通文件,即便用ls -l命令查看时,文件类型显示为 - 的文件;post
-d:测试其是否为目录文件,即便用ls -l命令查看时,文件类型显示为 d的文件;测试
-e:测试文件是否存在,不管是目录仍是文件,若是存在则为真,不然为假;spa
-r:测试文件对当前访问者来讲(非建立者)是否可读;rest
-w:测试文件对当前访问者来讲(非建立者)是否可写;ip
-x:测试文件对当前访问者来讲(非建立者)是否可执行;rpc
-s:测试文件是否有大小,若是有大小则为真,不然为假;
-l:测试文件是否为连接文件
例如要检验/tmp/test10是否不存在,若是不存在就建立之:
[root@localhost~]# vim if_exist.sh
#!/bin/bash # if [ ! -e /tmp/test10 ]; then #测试文件不存在,故对文件存在的状况取反 mkdir /tmp/test10 fi
[root@localhost~]# bash -x if_exist.sh
+ '[' '!' -e /tmp/test10 ']' + mkdir /tmp/test10
[root@localhost~]# ls /tmp
a.sh d.sh keyring-bgxXAq orbit-gdm pulse-yCmeAwocSW1U virtual-root.plTHoO yum.log b.sh e.sh keyring-Xi9NCS orbit-root test10 virtual-root.q9Sgpu c.sh f.sh keyring-xva5ss pulse-uP5T8Y6V6nIN virtual-root.nBhtJw virtual-root.rQ0Eab #test10这个目录已经建立了。
2、短路操做符
上面测试 /tmp/test10是否存在的例子,其实能够写做更简单的形式:[-e /tmp/test10 ] || mkdir /tmp/test10
这里的 || 就是短路操做符,意为或运算,即先测试|| 前面的语句,若是为真,就不测试 || 后面的语句了;若是|| 前面的语句为假,则继续测试 || 后面的语句。
短路操做符主要有如下几种:
&&: 与运算, 与运算的状况有:
真 && 真 = 真
真 && 假 = 假
假 && 真 = 假
假 && 假 = 假
||: 或运算,或运算的状况有:
真 || 真 = 真
假 || 真 = 真
真 || 假 = 真
假 || 假 = 假
注意,与运算具备优先级,即与运算先操做,或运算后操做
若是某个用户不存在,就建立该用户的例子,也能够用这种方式写:
id $UserName&> /dev/null || useradd $UserName
如今把这个例子复杂化:使用与运算和或运算的形式,测试一个用户是否存在,若是存在就显示其以存在,不然就建立这个用户:
[root@localhosttutor]# vim if_user6.sh
#1/bin/bash UserName=$1 ! id $UserName&> /dev/null && useradd $UserName || echo "$UserNameexists." #若是用户存在,则 && 前的部分为假,那么须要判断&& 后面的部分; #若是用户不存在,则 &&前面的部分为真,那么不须要判断&& 后面的部分 #对于 || 运算来讲,若是用户不存在,则前半部分都为假,则须要运行 || 后面的部分
[root@localhosttutor]# bash -x if_user6.sh root
+UserName=root + id root + echo 'rootexists.' root exists.
[root@localhosttutor]# bash -x if_user6.sh roott
+ UserName=roott + id roott + useraddroott
3、文件测试及短路运算综合实例
例1. 给定一个路径,判断若是为普通文件,显示之;若是为目录,显示之;不然显示为没法识别;
[root@localhost tutor]# vim if_file.sh
#!/bin/bash # if [ ! -e $1]; then echo "No such file." exit 7 fi # 先判断文件是否存在 if [ -f $1 ];then echo "Common file." elif [ -d $1]; then echo "Directory." else echo "Unknown file." fi
[root@localhost tutor]# bash if_file.sh /tmp
Directory.
[root@localhost tutor]# bash if_file.sh /etc
Directory.
[root@localhost tutor]# bash if_file.sh /etc/fstab
Common file.
[root@localhost tutor]# bash if_file.sh /etc/fstabb
No such file.
[root@localhost tutor]# bash if_file.sh /dev/sda
Unknown file.
例2. 写一个脚本,判断/var/log目录中全部文件的类型
[root@localhost tutor]# vim file_type.sh
#!/bin/bash # for File in/var/log/*; do if [ -f $File ]; then echo "$File: Commonfile." elif [ -d $File ]; then echo "$File:Directory." else echo "$File: Unknownfile." fi done
[root@localhost tutor]# bash file_type.sh
/var/log/anaconda.yum.log:Common file. /var/log/audit:Directory. /var/log/boot.log:Common file. /var/log/ConsoleKit:Directory.
例3. 写一个脚本:能够接受一个参数,其使用形式以下:
script.sh {start|stop|restart|status}
若是参数为start,建立空文件/var/lock/subsys/script,并显示“Starting scriptsuccessfully.”;
若是参数为stop,则删除文件/var/lock/subsys/script,并显示“Stop script finished.”;
若是参数为restart,则删除文件/var/lock/subsys/script后从新建立,并显示“Restarting scriptsuccessfully.”;
若是参数为status,那么:
若是/var/lock/subsys/script文件存在,则显示为“script is running.”
不然,则显示为“script is stopped.”
其它任何参数:则显示“script.sh{start|stop|restart|status}”
这里稍微补充一下,$0为脚本自身的名称,可是引用$0时,会引用全路径及名称,故若是只想引用脚本自己的名称,可使用basename $0。
[root@localhost tutor]# vim service.sh
——————————————如下为脚本内容——————————————————
#!/bin/bash # SvcName=`basename$0` # 使用basename取该脚本的基名 LockFile="/var/lock/subsys/$SvcName" # 提供一个锁文件的路径 if [ $# -lt 1]; then # 判断参数个数是否少于1,是则给用户提示信息,并退出脚本 echo "Usage: $SvcName {start |stop | restart | status}" exit 3 fi if [ $1 =='start' ]; then if [ -e $LockFile ]; then # 先判断该文件是否存在,若是存在,则不建立 echo "$SvcName isrunning." else touch $LockFile &>/dev/null # 若是参数为start,且文件不存在,则建立该文件 echo "Starting $SvcNamesuccessfully." fi elif [ $1 =='stop' ]; then if [ -e $LockFile ]; then # 若是传递的参数是stop,则先判断文件是否存在,若是存在就删除该文件 rm -f $LockFile &>/dev/null echo "Stopping $SvcNamefinished." else # 若是文件不存在,则无需删除,给出提示信息便可 echo "$SvcName is stoppedyet." fi elif [ $1 =='restart' ]; then rm -f $LockFile &> /dev/null touch $LockFile &> /dev/null # 若是参数为restart,则先删除该文件,再建立之 echo "Restarting $SvcNamesuccessfully." elif [ $1 =='status' ]; then # 若是参数为status,那么须要用到嵌套语句,考虑文件是否存在 if [ -e $LockFile ]; then echo "$SvcName isrunning." else echo "$SvcName isstopped." fi else echo "Usage: $SvcName {start |stop | restart | status}" exit 4 # 若是输入的参数不是这4个中的一个,给用户提示信息,并退出,且错误编号与无参数的状况不一样 fi
——————————————下面来测试该脚本——————————————————
[root@localhost tutor]# chmod +x service.sh
[root@localhost tutor]# ./service.sh
Usage:service.sh {start | stop | restart | status}
[root@localhost tutor]# echo $?
3
[root@localhost tutor]# ./service.sh start
Starting service.sh successfully.
[root@localhost tutor]# ls /var/lock/subsys
abrt-ccpp atd blk-availability cups local network pcscd rpc.statd sshd abrtd auditd certmonger haldaemon lvm2-monitor NetworkManager postfix rsyslog acpid autofs crond ip6tables messagebus openct rpcbind service.sh # 能够看到/var/lock/subsys下成功建立了service.sh文件
[root@localhost tutor]# ./service.sh stop
Stopping service.sh finished.
[root@localhost tutor]# ls /var/lock/subsys
abrt-ccpp atd blk-availability cups local network pcscd rpc.statd abrtd auditd certmonger haldaemon lvm2-monitor NetworkManager postfix rsyslog acpid autofs crond ip6tables messagebus openct rpcbind sshd # 传递的参数为stop,则强行删除了 service.sh文件
[root@localhost tutor]# ./service.sh restart
Restarting service.sh successfully.
[root@localhost tutor]# ls /var/lock/subsys
abrt-ccpp atd blk-availability cups local network pcscd rpc.statd sshd abrtd auditd certmonger haldaemon lvm2-monitor NetworkManager postfix rsyslog acpid autofs crond ip6tables messagebus openct rpcbind service.sh # 传递的参数为restart,强行删除了 service.sh文件后又建立了一遍
[root@localhost tutor]# ./service.sh status
service.sh isrunning. # 传递的参数为status,则显示当前文件存在与否
[root@localhost tutor]# ./service.sh stop
Stopping service.shfinished.
[root@localhost tutor]# ./service.sh status
service.sh isstopped.
[root@localhost tutor]# ./service.sh stat
Usage:service.sh {start | stop | restart | status} # 传递错误参数,给予提示信息后退出
[root@localhost tutor]# ./service.sh start
Startingservice.sh successfully.
[root@localhost tutor]# ./service.sh start
service.sh isrunning. # 文件已经存在了
[root@localhost tutor]# ./service.sh stop
Stoppingservice.sh finished.
[root@localhost tutor]# ./service.sh stop
service.sh isstopped yet. # 文件不存在
上述脚本是红帽系统遵循SysV风格启动服务时的经常使用启动格式。