[root@aliyun-ubuntu ~]# [ -f ./passwd.txt ] && echo file || echo 'not file'file[root@aliyun-ubuntu ~]# [ -d /etc/ ] && echo dir || echo 'not dir'dir[root@aliyun-ubuntu ~]# [ -d /etc/passwd ] && echo dir || echo 'not dir'
not dir
4.3 案例:书写脚本-检查文件类型
[root@aliyun-ubuntu /server/scripts]# cat check_type.sh #!/bin/bash############################################################### File Name: check_type.sh# Version: V1.0# Author: SunKexu# Organization: www.oldboyedu.com# Description:test file type##############################################################exportLANG=en.US_UTF-8# varsfile=$1# command# check param numif[$#-eq0];thenecho"Usage:$0 file/dir"exit1fi# soft linkif[-h$file];thenecho"$file is symbolic"exit0fi# fileif[-f$file];thenif[-x$file];thenmode="has permisson"elsemode="has not permission"fiif[-s$file];thensize="size not is 0"elsesize="size is 0"fiecho"${file} is file;permission:${mode};size:${size}"exit0fi# dirif[-d$file];thenecho"$file is directory"exit0fiecho"$file is other type file"[root@aliyun-ubuntu /server/scripts]# bash check_type.sh
Usage:check_type.sh file/dir
[root@aliyun-ubuntu /server/scripts]# bash check_type.sh /sbin
/sbin is symbolic
[root@aliyun-ubuntu /server/scripts]# bash check_type.sh ./check_type.sh
./check_type.sh is file;permission:has not permission;size:size not is 0[root@aliyun-ubuntu /server/scripts]# bash check_type.sh /usr/
/usr/ is directory
[root@aliyun-ubuntu /server/scripts]# bash check_type.sh ./test
./test is other typefile
4.4 逻辑运算
逻辑运算符
说明
-a
并且
-o
或者
!
取反
4.5 案例:书写脚本-检查服务是否正在运行或是开机自启动
[root@aliyun-ubuntu /server/scripts]# cat check_service2.sh #!/bin/bash############################################################### File Name: check_service2.sh# Version: V1.0# Author: SunKexu# Organization: www.oldboyedu.com# Description:############################################################### varsname=$1# commandif[$#-eq0];thenecho"Usage:$0 server name"exit1firunning=`systemctl is-active $name`enable=`systemctl is-enabled $name`if[${running}="active"-a${enable}="enabled"];thenecho"$name is running and enabled"elseecho"$name is not running or enabled"fi[root@aliyun-ubuntu /server/scripts]# bash check_service2.sh
Usage:check_service2.sh server name
[root@aliyun-ubuntu /server/scripts]# bash check_service2.sh sshd
sshd is running and enabled
[root@aliyun-ubuntu /server/scripts]# cat check_service.sh#!/bin/bash############################################################### File Name: check_service.sh# Version: V1.0# Author: SunKexu# Organization: www.oldboyedu.com# Description:check service status##############################################################exportLANG=en.US_UTF-8# varsservice=$1# command# check parameter numif[$#-eq0];thenecho"Usage:$0 service name"exit1fi# check running statestatus_run=`systemctl is-active ${service}`if[${status_run}="active"];thenecho"${service} is running"elseecho"${service} is not running"fi# check enabled statestatus_enabled=`systemctl is-enabled ${service}`if[${status_enabled}="enabled"];thenecho"${service} is enabled"elseecho"${service} is not enabled"fi#####################################if[${status_run}="active"-a${status_enabled}="enabled"];thenecho"$service is active and enabled"fi[root@aliyun-ubuntu /server/scripts]# bash check_service.sh sshd
sshd is running
sshd is enabled
sshd is active and enabled
[root@aliyun-ubuntu /server/scripts]# cat check_disk.sh#!/bin/bash############################################################### File Name: check_disk.sh# Version: V1.0# Author: SunKexu# Organization: www.oldboyedu.com# Description:##############################################################exportLANG=en.US_UTF-8# varsusage=`df-h / |awk-F'[ %]''NR==2{print $(NF-2)}'`# ifif[$usage-gt60-a$usage-le70];thenecho"Warning: Insufficient disk space ${usage}"elif[$usage-gt70-a$usage-le80];thenecho"Warning: Insufficient disk space ${usage}"elif[$usage-gt80-a$usage-le95];thenecho"Warning: Severe shortage of disk space ${usage}"elif[$usage-gt95];thenecho"Warning: Disk space is about to run out ${usage}"elseecho"Disk space is normal"fi[root@aliyun-ubuntu /server/scripts]# bash check_disk.sh
Disk space is normal