數學比較運算
| 符號 | 說明 | 符號 | 說明 |
| :--: | :---: | :--: | :---: |
| -lt | 小于 | -gt | 大于 |
| -le | 小于或等于 | -ge | 大于或等于 |
| -eq | 等于 | -ne | 不等于 |
`$?`上一個命令執行后的退出狀態,`0`表示成功,其他表示失敗。
```bash
[root@HongKong ~]# test 2 -gt 1
[root@HongKong ~]# echo $?
0
[root@HongKong ~]#
```
文件比較運算
| 符號 | 說明 | 符號 | 說明 |
| :--: | :--------: | :-------: | :--------: |
| -d | 存在且為目錄 | -e | 文件存在 |
| -f | 存在且為文件 | -r | 存在且可讀 |
| -s | 存在且不為空 | -w | 存在且可寫 |
| -x | 存在且可執行 | -O | 存在且被當前用戶擁有 |
| -G | 存在且默認組為當前組 | f1 -nt f2 | 檢查f1比f2新 |
| | | f1 -ot f2 | 檢查f1比f2舊 |
查看手冊
```bash
man test
```
語法一:單if語句
```bash
if [ condition ]
then
commands
fi
```
```bash
if [ -e /etc/profile ]
then
echo "The file is exist1."
echo "The file is exist2."
echo "The file is exist3."
fi
```
語法二:if-then-else
```bash
if [ condition ]
then
commands
else
commands
fi
```
語法三:if-then-elif
```bash
if [ condition ]
then
commands
elif [ condition ]
then
commands
else
commands
fi
```