先看一些使用:
[root@centosTest shelltest]# expr 10 * 10 expr: syntax error [root@centosTest shelltest]# expr 10 \* 10 100 [root@centosTest shelltest]# expr "10 * 10" 10 * 10 [root@centosTest shelltest]# expr "10 \* 10" 10 \* 10 [root@centosTest shelltest]# expr 10 \ 2 expr: syntax error [root@centosTest shelltest]# expr 10 / 2 5 [root@centosTest shelltest]# expr 10 /* 2 expr: syntax error [root@centosTest shelltest]# expr 10 \* 2 20 [root@centosTest shelltest]# let "add=1+1" [root@centosTest shelltest]# echo $add 2 [root@centosTest shelltest]#
let要把表达式用引号引起来,expr则相反!
$(())
[root@centosTest shelltest]# i=$((i+1)) [root@centosTest shelltest]# echo $i 1 [root@centosTest shelltest]# i=$((1+3)) [root@centosTest shelltest]# echo $i 4 [root@centosTest shelltest]# j=$((3*2)) [root@centosTest shelltest]# echo $j 6 [root@centosTest shelltest]#
expr+函数
[root@centosTest shelltest]# expr length "wo shi f" 8 [root@centosTest shelltest]# expr substring "wo shi fu wenchao" 11 18 expr: syntax error [root@centosTest shelltest]# expr substr "wo shi fu wenchao" 11 18 wenchao [root@centosTest shelltest]# expr index "woshiwoshi" o 2 [root@centosTest shelltest]#
增量计数:
loop=0 loop=`expr $loop + 1`
[root@centosTest shelltest]# loop=`expr $loop + 1` [root@centosTest shelltest]# echo $loop 1 [root@centosTest shelltest]# loop=`expr $loop + 1` [root@centosTest shelltest]# echo $loop 2 [root@centosTest shelltest]# loop=`expr $loop + 1` [root@centosTest shelltest]# echo $loop 3 [root@centosTest shelltest]#
[root@centosTest shelltest]# num=1 [root@centosTest shelltest]# scale[1]=3 [root@centosTest shelltest]# num=`expr $num + ${scale[1]}` [root@centosTest shelltest]# echo $num 4 [root@centosTest shelltest]#
[root@centosTest shelltest]# scale[1]=3 [root@centosTest shelltest]# num=`expr $num + ${scale[1]}` [root@centosTest shelltest]# echo $num 4 [root@centosTest shelltest]# scale[2]=4 [root@centosTest shelltest]# scale[3]=5 [root@centosTest shelltest]# echo ${scale[*]} 3 4 5 [root@centosTest shelltest]#
输出导入/dev/null
[root@centosTest shelltest]# value=11 [root@centosTest shelltest]# expr $value + 1>/dev/null 2>&1 [root@centosTest shelltest]# echo $? 2 [root@centosTest shelltest]# value=11 [root@centosTest shelltest]# expr $value + 1 >/dev/null 2>&1 [root@centosTest shelltest]# echo $? 0
expr操作字符串
expr也操作用于字符串
字符串比较: expr 如果成功,返回值1,任何其他值为无效或错误。
如下面的例子测试两个字符串是否相等,这里字符串为“hello”和hello”。
[root@centosTest shelltest]# value=hello [root@centosTest shelltest]# expr $value = "hello" 1 [root@centosTest shelltest]# echo $? 0
expr返回1。不要混淆了,这表明成功。现在检验其最后退出状态,返回0表示测试成功,
“hello”确实等于“hello”。
expr的模式匹配:
关于expr模式匹配,可以使用expr通过指定冒号:选项指定要进行字符串模式匹配。 .*代表任
何字符重复0次或多次。返回值为括号中的内容.
[root@centos-fuwenchao ~]# value=accounts [root@centos-fuwenchao ~]# expr $value : '.*' 8 [root@centos-fuwenchao ~]# expr $value : '(.*).doc' 0 [root@centos-fuwenchao ~]# echo $value accounts.doc [root@centos-fuwenchao ~]# expr $value:'(.*).doc' accounts.doc:(.*).doc [root@centos-fuwenchao ~]# expr $value : '(.*).doc' 0 [root@centos-fuwenchao ~]#
如在tomcat的catalina.sh,使用了expr的模式匹配来获取连接文件的真实路径,例子如下:
# resolve links - $0 may be a softlink PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-< \(.*\)$'` #这里expr的返回值为连接文件的真实路径 if expr "$link" : '/.*' < /dev/null; then #路径名返回1 PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done
expr数值测试
[root@centos-fuwenchao ~]# rr=3.4 [root@centos-fuwenchao ~]# expr $rr + 1 expr: non-numeric argument [root@centos-fuwenchao ~]# rr=5 [root@centos-fuwenchao ~]# expr $rr + 1 6 [root@centos-fuwenchao ~]#
转载于:https://blog.51cto.com/fuwenchao/1351065