四天精通shell编程(三)

四条件测试

成都创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为西丰企业提供专业的网站制作、成都做网站,西丰网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。


test测试条件 测试内容

[测试条件 测试内容] #用的更为广泛


1测试文件类型


test -e文件名测试文件是否存在,存在为真

[ -e文件名]注意:[]的左右两侧必须加空格!


附:test测试结果并不显示,需用echo$?来显示test的返回值

返回0语句正确,非0表示不正确。

拓展:test-e test.txt && echo Yes || echo No

test -f文件名判断是否是普通文件#如果是文件的话,返回0,若是目录返回非0

test -d文件名判断是否为目录

test -b文件名判断是否为块设备文件

test -c文件名字符设备文件


2测试文件权限


test -r文件名判断是否有可读权限

test -w文件名可写

test -x文件名执行

test -s文件名判断是否为非空白,有内容为真


3两个文档比较


[file1 -nt file2 ] file1是否比file2新

[file1 -ot file2 ] file1是否比file2旧

[file1 -ef file2 ] file1与file2是否是链接文件,是否是快捷方式,软链接


4两个数值之间判断

[n1 -eq n2 ] n1和n2是否相等#推荐使用下面的==符号来判断

测试:aa=123;bb=123

[$aa -eq $bb ] && echo Yes || echo no

[n1 -ne n2 ] n1和n2是否不等

[n1 -gt n2 ] n1大于n2

[n1 -lt n2 ] n1小于n2

[n1 -ge n2 ] n1大于等于n2

[n1 -le n2 ] n1小于等于n2



5判断字符串


[-z字符串]判断字符串是否为空,变量值没有内容/为空则是真


[字符串1 ==字符串2]判断字串1是否与字串2相等#判断采用的是字符串型

[$aa == $bb ] && echo 1 || echo 2

附:aa=abc

[“$aa” == “abc” ] && echo Yes || echo No #为什么把两个“”去掉测试也成功


[字符串1 !=字符串2]判断字串是否不等


6多重判断


-a逻辑与

[-z $file -a -e $file ]

-o逻辑或

!逻辑非


例子7:判断输入的文件类型和文件权限注:一定要练习

#!/bin/bash


echo-e "nide wenjian shifou cunzai? wenjian quanxian shi shenme?\n\n"


read-p "please input filename:" -t 30 filename


test-z $filename && echo "please input filename!!" &&exit 1

#判断字符串是否为空,如果为空,打印错误信息,并且退出程序!


test! -e $filename && echo "wenjian bucunzai!" &&exit 2

#-e变量判断文件是否存在,存在为真! 逻辑非


test-f $filename && filetype=putong

#-f是否为普通文件

test-d $filename && filetype=mulu

#-d是否为目录

test-r $filename && perm="read"

#-r是否有可读权限

test-w $filename && perm="$perm write"

#-w是否可写

test-x $filename && perm="$perm executable"

#-x是否可执行


echo-e "the filename is : $filename \n"

#打印文件名

echo-e "filetype is : $filetype \n"

#打印文件类型

echo-e "permision is : $perm \n"

#打印文件权限


五流程控制


1 if语句


1)if条件语句--单分支。当“条件成立”时执行相应的操作

格式:

if条件测试命令

then命令序列

fi


例子8:

如果/boot分区的空间使用超过80%,则输出警告

#!/bin/bash

RATE=`df-hT | grep "/boot" | awk '{print $6}' | cut -d "%"-f1 ` #:反引号!!!

#:df命令:显示分区使用状况

if [ $RATE -gt 80 ]

then

echo"Warning,/boot DISK is full!"

fi


2)if条件语句--双分支。当“条件成立”、“条件不成立”时执行不同操作

格式:

if条件测试命令

then命令序列1

else命令序列2

fi


例子9:

判断httpd服务是否启动,如果没有启动则启动

#!/bin/bash


http=`netstat-tlun | awk '{print $4 "\n"}' | grep ":80$"`

(或http=$(ps aux | grep httpd | grep -v grep))


附:netstat-tlun用来查看系统启动的所有端口

psaux用来打印apache所有的进程

grep-v grep把包含有grep的行取反

if [ -z "$http" ]

then

echo"httpd meiyou qidong!"

/etc/rc.d/init.d/httpdstart

else

echo"httpd runing"

fi

附:停掉Apache:servicehttpd stop

3) if条件语句--多分支

格式:

if条件测试命令1 ; then

命令序列1

elif条件测试命令2 ; then

命令序列2

elif ...

else

命令序列n

fi


例子10:

#判断输入的字符

#!/bin/bash

echo"if you want to beijing ,please input 1"

echo"if you want to shanghai ,please input 2"

echo"if you want to chengdu ,please input 3"



read-p "please input a num: " -t 30 num


if[ "$num" == "1" ]

then

echo"beijing!!!"

elif[ "$num" == "2" ]

then

echo"shanghai!!!!"

elif[ "$num" == "3" ]

then

echo"chengdu!!!"

else

echo"error,please input 1 or 2 or 3."

fi













当前名称:四天精通shell编程(三)
网页网址:http://csdahua.cn/article/jsppsh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流