Linux鸟哥视频学习笔记30-31

shell脚本 第二部分


test判断


 
实操
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"

两个整数之间的判断

 

实操
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"

判断字符串的数据


实操
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"

多重条件判定 

 

实操
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"

 

脚本:使用test输出文件的属性


实操
vi sh05.sh
#!/bin/bash
echo "this script will show you filetype and permission"
read -p "Please input a file name:" filename
test -z "$filename" && echo "Please input something !!!" exit 0

test -f "$filename" && filetype="is a regular file"
test -d "$filename" && filetype="is a directory"
test -p "$filename" && filetype="is a PIPE file"
test -b "$filename" && filetype="is a block device"
test -c "$filename" && filetype="is a character device"
test -S "$filename" && filetype="is a socket file"
test -L "$filename" && filetype="is a link file"

test -r "$filename" && per="$per readable"
test -w "$filename" && per="$per writable"
test -x "$filename" && per="$per executable"
test -u "$filename" && per="$per set UID"
test -g "$filename" && per="$per set GID"
test -k "$filename" && per="$per sticky bit"

echo "the file $filename you have input $filetype,and the permission is  $per"

sh sh05.sh  执行


判断符号[]



test -r sh01.sh && echo "readable" || echo "not readable"
[  -r sh01.sh  ] && echo "readable" || echo "not readable" 
[  -d Desktop  ] && echo "is a directory" || echo "is not a directory"

注意:[]中必须注意严格的语法格式,必须要在"["后面和"]"前面加空格 否则报错 少空格会提示语法错误

脚本:使用[]输出文件的属性


vi sh05-2.sh

sh sh05-2.sh

脚本:使用[]判断输入的内容


vi sh06.sh

#!/bin/sh
read -p "Please input a file name:" filename

[  -z "$filename"  ] && echo "you should input something" && exit 0
[  ! -e "$filename"  ] && echo "the file $filename DO NOT exist" && exit 0

test -r "$filename" -o -w "$filename" && per1="readable or writable"
[  -r "$filename" -a -x "$filename"  ] && per2="readable and executable"

echo "the file $filename is $per1,$per2"

sh sh06.sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值