五、比较
相等比较 str1 = str2
不等比较 str1 != str2
举例:
if [ "$USER_NAME" = "terry" ]; then echo "I am terry" fi |
小于比较
#return 0 if the two string is equal, return 1 if $1 < $2, else 2strCompare() { local x=0 if [ "$1" != "$2" ]; then x=2 localTEMP=`printf "%s\n%s" "$1" "$2"` local TEMP2=`(echo "$1"; echo "$2") |sort` if [ "$TEMP" = "$TEMP2" ]; then x=1 fi fi echo $x } |
六、测试
判空 -z str
判非空 -n str
是否为数字
# return 0 if the string is num, otherwise 1 strIsNum() { local RET=1 if [ -n "$1" ]; then local STR_TEMP=`echo "$1" | sed 's/[0-9]//g'` if [ -z "$STR_TEMP" ]; then RET=0 fi fi echo $RET } |
举例:
if [ -n "$USER_NAME" ]; then echo "my name is NOT empty" fi echo `strIsNum "9980"` |