举例: if [ "$USER_NAME" = "terry" ]; then echo "I am terry" fi
小于比较 # return 0 if the two string is equal, return 1 if $1 < $2, else 2 strCompare() { local x=0 if [ "$1" != "$2" ]; then x=2 local TEMP=`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"`
分割 以符号+为准,将字符分割为左右两部分 使用sed 举例: 命令 date --rfc-3339 seconds 的输出为 2007-04-14 15:09:47+08:00 取其+左边的部分 date --rfc-3339 seconds | sed 's/+[0-9][0-9]:[0-9][0-9]//g' 输出为 2007-04-14 15:09:47 取+右边的部分 date --rfc-3339 seconds | sed 's/.*+//g' 输出为 08:00