条件测试使用方式:express
test expression 或ide
[::expression::] 或测试
[[::expression::]]字符串
说明:it
“test”和”[“ 均为命令,”[[”是关键字io
上面的形式中的□为空格,必须for循环
整数测试class
-eq 等于 [ "$a" -eq "$b" ]test
-ne 不等于 [ "$a" -ne "$b" ]file
-gt 大于 [ "$a" -gt "$b" ]
-ge 大于等于 [ "$a" -ge "$b" ]
-lt 小于 [ "$a" -lt "$b" ]
-le 小于等于 [ "$a" -le "$b" ]
<, <=,>, >= 此些须要在双括号“(())”中才能使用
(( “$A” > “$B“ ))
字符串测试
= 等于 [ "$a" = "$b" ]
!= 不等于 [ "$a" != "$b" ]
< 小于 [ “$a” \< “$b” ] ,转义
> 大于 [[ “$a” > “$b” ]]
-z 字符串为"null", 意思就是字符串长度为零
-n 字符串不为"null"
文件测试
-f file 测试文件是否为普通文件
-d dir 存在且为一个目录时为真
-h或-L file 测试文件是否为符号连接
-r 文件是否具备可读权限(正在运行此测试命令的用户)
-w 文件是否具备可写权限(正在运行此测试命令的用户)
-x 文件是否具备可执行权限(正在运行此测试命令的用户)
-e 判断文件是否存在
-s 判断文件大小是否为0,不为0时返回真
f1 -nt f2 文件f1比文件f2新
f1 -ot f2 文件f1比文件f2旧
f1 -ef f2 文件f1和文件f2是相同文件的硬连接
! "非" -- 反转上边全部测试的结果
if循环格式(特别注意if elif 后均须要加then)
if [ condition1 ]
then
command
…
elif [ condition2 ]
then
command
…
else
command
…
fi
for循环
for arg in [list]
do
command
…
done
while循环 须要注意的是对判断条件的修改
while [conditions] while在知足condition时执行条件,不知足是不执行do循环
do
command
…
done
until循环
until [conditions]
until 在不知足conditions条件时,执行循环,不知足不执行都正好与while相反
do
command
…
done