Linux鸟哥视频学习笔记30

shell脚本 第二部分


test判断
Linux鸟哥视频学习笔记30


 
实操
test -d Music && echo "is a dir" || echo "is not a dir" 判断是否为文件夹
test -f sh01.sh && echo "is a regulare file" || echo "is not a file"
test -e sh06.sh && echo "exist" || echo "not exist"
ll /dev | grep "^b"
test -b /dev/sda && echo "is a block device" || echo "is not a block device"
test -p /dev/initctl && echo "is a FIFO file" || echo "is not a FIFO file" 
test -S /dev/log && echo "is a socket file" || echo "is not socket file" 判断是否是套接字文件
test -r sh01.sh && echo "readable" || echo "not readable" 判断是否有可读权限
test -u sh01.sh && echo "set UID" ||echo "have not set UID" 
test -s sh06.sh && echo "exist or size>0" || echo "not exit or size=0"
两个文档之间的比较 
test sh01.sh -ot sh02.sh && echo "old" || echo "not old"
test sh02.sh -nt sh02.sh && echo "newer" || echo "not newer"

两个整数之间的判断
Linux鸟哥视频学习笔记30

 

实操
per1=10
per2=12
test "$per1" -eq "$per2" && echo "equal" || echo "not equal"
test "$per1" -ge "$per2" && echo "greater or equal" || echo "not greater or equal"

判断字符串的数据
Linux鸟哥视频学习笔记30


实操
str=""
test -z "$str" && echo "is null" || echo "is not null"
test -n "$str" && echo "is a string" || echo "is a null string"
str1="abc"
test "$str" = "$str1" && echo "is equal" || echo "is not equal"

多重条件判定 
Linux鸟哥视频学习笔记30

 

实操
test -r sh01.sh -a -x sh01.sh && echo "readable and executable" || echo "no"
test -r sh03.sh -o -x sh03.sh && echo "reeadable or executable" || echo "no"
test ! -x sh03.sh && echo "not have executable" || echo "have executable"