shell 條件語句
#!/bin/bash
# 條件語句
NUM1=100
NUM2=200
if (($NUM1 > $NUM2));then
echo "$NUM1 greater than $NUM2 !"
else
echo "$NUM1 less than $NUM2 !"
fi
判斷目錄是否存在,判斷檔案是否存在
-f 判斷檔案 中括号
-d 判斷目錄
-a and
-o or
-z 空字元串
-eq 等于
-ne 不等于
-lt 小于
-gt 大于
-le 小于等于
-gt 大于等于
#!/bin/bash
# 條件語句
NUM1=100
NUM2=200
if [ $NUM1 -gt $NUM2 ];then
echo "$NUM1 greater than $NUM2 !"
else
echo "$NUM1 less than $NUM2 !"
fi
#!/bin/bash
# 條件語句
if [ ! -d "doc" ];then
mkdir doc
echo "目錄建立成功"
else
echo "目錄已存在"
fi
注意空格。
#!/bin/bash
# 條件語句
if [ ! -f "test.txt" ];then
touch test.txt
echo "檔案建立成功"
else
echo "檔案已存在"
fi
> 覆寫
>> 追加
#!/bin/bash
# 條件語句
score=85
if [ $score -gt 80 ];then
echo "very good"
elif [ $score -gt 75 ];then
echo "good"
elif [ $score -gt 60 ];then
echo "pass"
else
echo "not pass"
fi
#!/bin/bash
# 條件語句
score=85
if [[ $score -gt 80 ]];then
echo "very good"
elif [[ $score -gt 75 ]];then
echo "good"
elif [[ $score -gt 60 ]];then
echo "pass"
else
echo "not pass"
fi
推薦使用雙中括号。