shll ----- 条件判断

test命令

test “ a " = = " a" == " b” 等同于 [ “ a " = = " a" == " b” ]

[ “ a " = " a" = " b" ] 等于
[ “ a " ! = " a" != " b” ] 不等于
[ “ a " e q " a" -eq " b” ] 等于
[ “ a " n e " a" -ne " b” ]不等于
[ “ a " l e " a" -le " b” ] 小于等于
[ “ a " g e " a" -ge " b” ] 大于等于
[ “ a " g t " a" -gt " b” ]大于
[ “ a " l t " a" -lt " b” ] 小于
[ “ a " n e " a" -ne " b” -a “ a " g t " a" -gt " b” ]-a必须条件都满足
[ “ a " n e " a" -ne " b” -o" a " g t " a" -gt " b" ] -a条件至少满足一个
[ -z “$a” ] 是否为空
[ -e “file” ] 是否存在
[ -f “file” ] 普通文件
[ -b “file” ] 块设备
[ -S “file” ] 套接字
[ -c “file” ] 字符设备
[ -L “file” ] 软链接

练习
判断输入的数字是否在10以内
1.输入是否为空
2.是否在10以内
3.1< a &lt; 10 &gt; y e s 4. a&lt;10 --&gt; yes 4. a<1 $a>10 --> no

在这里插入图片描述

[root@localhost mm]# sh westos3.sh 
please input a number!
[root@localhost mm]# sh westos3.sh 3
YES
[root@localhost mm]# sh westos3.sh 20
NO
[root@localhost mm]# 

练习
判断文件类型

#!/bin/bash
[ -z “$1” ] && {
echo “please enter the file name!”
exit 1
}
[ -e “$1” ] || {
echo “This file does not exist!”
exit 1
}
[ -f “$1” ] && {
echo “The file is a normal file.”
}
[ -b “$1” ] && {
echo “The file is a block device.”
}
[ -S “$1” ] && {
echo “The file is a socket.”
}
[ -c “$1” ] && {
echo “The file is a character device.0”
}
[ -L “$1” ] && {
echo “The file is a soft link”
}

猜你喜欢

转载自blog.csdn.net/wuludan0217/article/details/85314778