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