== 等于則條件為真
!= 不等于條件則為真
-z? ? ? 判斷值是否為空,為空則為真,字符串的長(zhǎng)度為零
-n? ? ? 判斷字符串的長(zhǎng)度不為零則為真
字符比較要用雙引號(hào)括起來(lái)
[root@shell /scripts]# echo $USER
root
[root@shell /scripts]# echo $SHELL
/bin/bash
[root@shell /scripts]# [ "$USER" == "root" ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ "$USER" == "roott" ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ "$USER" != "roott" ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ "$USER" != "root" ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# name=""
[root@shell /scripts]# echo ${#name}
0
[root@shell /scripts]# [ -z $name ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# name=1
[root@shell /scripts]# echo ${#name}
1
[root@shell /scripts]# [ -z $name ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ -n $name ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# cat yes.sh
#!/bin/bash
read -p "請(qǐng)輸入一組字符串(如:yes或者no):" Zf
if [ "$Zf" == "yes" ];then
? ? echo "你輸入的是yes"
elif [ "$Zf" == "no" ];then
? ? echo "你輸入的是no"
else
? ? echo "你輸入的不正確"
fi
[root@shell /scripts]# sh yes.sh
請(qǐng)輸入一組字符串(如:yes或者no):yes
你輸入的是yes
[root@shell /scripts]# sh yes.sh
請(qǐng)輸入一組字符串(如:yes或者no):no
你輸入的是no
[root@shell /scripts]# sh yes.sh
請(qǐng)輸入一組字符串(如:yes或者no):yess
你輸入的不正確
[root@shell /scripts]# sh yes.sh
請(qǐng)輸入一組字符串(如:yes或者no):noo
你輸入的不正確