bash条件测试及变量比较
一、 条件表达式
1. 基本的if语句 if [ … ] then …. Fi
2. 多个if语句 if […] then … Elif […] Then … Fi
3. 嵌套if语句 If […] Then … If[…] Then … Fi Else … fi |
二、 字符串和数字比较
| Number | String |
大于 | -gt | /> |
小于 | -lt | /< |
等于 | -eq | == |
大于等于 | -ge | Single[] doesnot support |
小于等于 | -le | Single[] doesnot support |
不等于 | -ne | != |
三、 关键点
1. if和elif后面有then
2. else if是非法的
3. 单个中括号[]里面的”<”和“ >”需要转义,且不支持”>=” 和“<=”
4. “z” 测试字符串是否为NULL
5. “n” 测试字符串是否不为NULL
四、 代码
#! /bin/bash echo "test you" a=12 b=13
# number compare # -eq: equal # -ne: not equal # -gt: great # -ge: great and equal # -lt: little # -le: little and equal if [ "$a" -ne "$b" ] then echo "$a is not equel to $b" fi
if [ "$a" -eq "$b" ] then echo "$a is equal to $b" else echo "$a is not equal to $b" fi
if [ "$a" -eq "$b" ] then echo "$a is equal to $b" elif [ "$a" -lt "$b" ] then echo "$a is little to $b" elif [ "$a" -gt "$b" ] then echo "$a is great to $b" else echo "Default" fi #########Key Point############## #########1."then" after "if" or "elif"############ #########2."elif" is ok, not "else if"############
c=abc d=abd e= #string compare
if [ "$c" == "$d" ] then echo "string $c is equal to $d" elif [ "$c" /> "$d" ] then echo "string $c is great to $d" elif [ "$c" /< "$d" ] then echo "string $c is little to $d" elif [ "$c" != "$d" ] then echo "string $c is not equal to $d" fi
if [ -z "$e" ] then echo " $e is NULL" else echo " $e is not NULL" fi
if [ -n "$e" ] then echo "$e is not NULL" else echo "$e is NULL" fi ###########KEy point################# ###########1.For string comparing, ">" "<"need escape symbol ###########2."z": zero is used to test whether the string is NULL ###########3."n": not zero is used to test whether the string is not NULL
f=8 if [ "$f" -gt 0 ] then echo "f is positive" if [ "$f" -gt 5 ] then echo "f is a big positive" else echo "f is a small positive" fi else echo "f is negative" if [ "$f" -gt -5 ] then echo "f is a big negative" else echo "f is a small negative" fi fi |