## if
判斷一個文件是否存在
```
[ -f /etc/passwd ] && echo "yes" || echo "no"
```
判斷一類文件(*.c)是否存在
```
[ $(ls *.c 2> /etc/null) ] && echo "yes" || echo "not"
```
判斷一個文件是否存在(腳本)
```
#!/bin/bash
if [ -f /etc/passwd ]; then
echo "there"
else
echo "not there"
fi
```
判斷遠程服務器端口是否通
```
#!/bin/bash
nc -zv www.baidu.com 80 &> /dev/null
if [ $? -eq 0 ]
then
echo "up"
else
echo "down"
fi
```
自定義
```
#!/bin/bash
result="fail"
if [ $result == "success" ]
then
echo "success"
else
echo "fail"
fi
```
與 或
```
if [[ $a != 1 && $a !=2 ]]
if [[ $a != 1 || $a !=2 ]]
```
## case
case案例
```
#!/bin/bash
read -p "Please enter your city:" your_city
echo -n "The prefix phone number of $your_city is "
case $your_city in
Beijing | BEIJING | beijing)
echo "010"
;;
Shanghai | SHANGHAI | shanghai)
echo "021"
;;
Shenzhen | SHENZHEN | shenzhen)
echo "0755"
;;
*)
echo "to be continued..."
;;
esac
```