1、条件测试:
格式①:test<条件表达式>
格式②:[<条件表达式>]
格式③:[[<条件表达式>]]
①和②是等价的,③为扩展的test命令
#①eg:
touch file
test -f file #判断file(文件)是否存在
test -f file && echo 1 || echo 0 #结果:1
rm -rf file
[ -f file ] && echo 1 || echo 0 #0
touch file
[[ -f file ]] && echo 1 || echo 0 #1
#②test命令!(逻辑非)的写法:
touch file
test ! -f file && echo 1 || echo 0 #0
rm -rf file
[ ! -f file ] && echo 1 || echo 0 #1
touch file
[[ ! -f file ]] && echo 1 || echo 0 #0
双中括号与单中括号的区别在于:
双中括号中可以使用的的操作符更多一些(&&、||、>、<):
touch file dir
[[ -f file && -d dir]] && echo 1 || echo 0 #0
rm -f dir;mkdir dir
[[ -f file && -d dir]] && echo 1 || echo 0 #1
#单中括号中不能使用&&但是能用-a(and)代替&&,-o(or)代替||
[ -f file && -a -d dir] && echo 1 || echo 0 #1
2、常用文件测试操作符号:
f file:文件存在且为普通文件则为真(f:file)
-d file:文件存在且为目录文件则为真(d:directory)
-s file:文件存在且文件大小(不为空)不等于0则为真(s:size)
-e file:文件存在即为真(e:exist)
-r file:文件存在且可读则为真(r:read)
-w file:文件存在且可写则为真(w:write)
-x file:文件存在且可执行则为真(x:executable)
-L file:文件存在且为链接文件则为真(L:link)
f1 -nt f2:文件f1比文件f2新则为真(nt:newer than)
f1 -ot f2:文件f1比文件f2旧则为真(ot:older than)
测试-x:
测试-d,-f:
3、字符串测试操作符:
-z "string" #字符串string长度为0则为真(zero)
-n "string" #字符串string长度不为0则为真(not zero)
"string1"=="string2" #字符串1与字符串2相同为真
"string1"!="string2" #字符串1与字符串2不相同为真
测试-z、-n:
注意:对于变量$string需要加引号目的是防止错误发生。因为解析出来的变量内容作为文件名如果文件名中带空格,不加引号就会有问题。
关于有无引号会引起的问题,测试如下:
4、整数比较二元操作符:
注意:这里说的是用来比较整数的二元操作符
在[]中使用的比较符 : 在(())和[[]]中使用的比较符
-eq (equal) : ==(“=”在“(())”中无法使用)
-ne (not equal) : !=
-gt (greater than) : >
-ge (greater equal) : >=
-lt (lesser than) : <
-le (lesser euqal) : <=
在[]中使用的比较符 : 在[[]]中使用的比较符
-a &&
-o ||
! !
在单中括号里面也可以使用“>”“<”“=”(“==”)“!=”四个运算符,但是“>”和“<”需要转义,否则会将“>”和“<”识别成重定位符号,而“>=”和“<=”不能用在[]中。(“=”和”==”在一定条件下都可以判断相等否,但为了与赋值运算区别开,统一使用“==”)
5、“[]”和“[[]]”:
对于&&、||的三种写法:
#测试:
file1=/etc/passwd #passwd为普通文件
file2=/etc/opt #opt为目录文件
[ -d "$file1" -a -d "$file2" ] && echo 1 || echo 0 #0,[]中用-a代替&&
[[ -d "$file1" && -d "$file2" ]] && echo 1 || echo 0 #0,[[]]中可以直接用&&
[ -d "$file1" ] && [ -d "$file2" ] && echo 1 || echo 0 #0,[]分成两段而用&&连接
[ -d "$file1" -o -d "$file2" ] && echo 1 || echo 0 #1
[[ -d "$file1" || -d "$file2" ]] && echo 1 || echo 0 #1
[ -d "$file1" ] || [ -d "$file2" ] && echo 1 || echo 0 #1
6、简单应用:
(1)、判断指定路径下是否存在指定文件,存在打印提示信息,不存在则创建
#!/bin/bash
#在命令行输入一个存在的路径,和一个文件名file
#如果目录下存在file就打印存在exist,不存在就创建文件并打印成功创建的信息
while true
do
read -p "Please input a path of directory:" path
[ $? -ne 0 ] && continue
ls $path >& /dev/null
[ $? -ne 0 ] && { #ls出错说明路径不存在
echo "No have this path,please input again!"
continue
}
read -p "Please input a filename:" file
[ $? -ne 0 ] && continue || break
done
#方法一:直接用条件测试做
if [ -f "$path$file" ]
then
echo "exist"
else
echo "not exist"
touch $file && echo "$file is create success"
fi
#方法二:用循环做
#flags=0
#for name in `ls $path`
#do
# if [ "$name" == "$file" ]
# then
# flags=1;break
# fi
#done
#[ $flags -eq 1 ] && echo "exist" ||{
# echo "not exist"
# touch $file && echo "$file is create success"
#}
(2)、判断一个参数是否是整数:
#方法①:
#a=1234
#a=1234k
expr $a + 0 >& /dev/null
[ $? -eq 0 ] && echo "整数" || echo "非整数"
#方法②剔除数字部分看是否为空(不能判断出负数):
[ -n "`echo $a | sed 's/[0-9]//g'`" ] && echo "正整数" || echo "非正整数"
#方法③剔除非数字部分,看是否变化(不能判断出负数):
[[ -n "$a" && "$a" == "${num//[^0-9]/}" ]] && echo "正整数" || echo "非正整数"