shell 之if中数字比较
2015-02-12 13:23
120 查看
dash (ubuntu 中sh 默认连接到dash)好像没有“==” ,不过bash有,下面代码会有报错。
把sh改成bash 就不会有问题
参考:
简版:http://zhidao.baidu.com/link?url=WSUZ_h7lRKlWqjRi6fMvcYk0hxg9nWzBfW4xZTLNURF4BjlQWbbM4Wfx6NrcVfL2sa9zu2DO5rQn2kFSvmp0ha
完整版:http://www.jb51.net/article/34332.htm
把sh改成bash 就不会有问题
#!/bin/sh xxx=10 if [ $xxx -gt 20 ] then echo 'xxx>20' fi if [ $xxx -lt 20 ] then echo 'xxx<20' fi if [ $xxx -gt 5 ] then echo 'xxx>5' fi if [ $xxx -lt 5 ] then echo 'xxx<5' fi if [ $xxx -eq 10 ] then echo xxxeq10 fi if [ $xxx -ne 10 ] then echo xxxne10 fi if [ $xxx -eq 20 ] then echo xxxeq20 fi if [ $xxx -ne 20 ] then echo xxxne20 fi if [ $xxx == 10 ] then echo 'xxx==10' fi if [ $xxx = 10 ] then echo 'xxx=10' fi if [ $xxx != 10 ] then echo 'xxx!=10' fi if [ ! $xxx = 10 ] then echo '!xxx=10' fi if [ ! $xxx == 10 ] then echo '!xxx==10' fi if [ $xxx == 20 ] then echo 'xxx==20' fi if [ $xxx = 20 ] then echo 'xxx=20' fi if [ $xxx != 20 ] then echo 'xxx!=20' fi if [ ! $xxx = 20 ] then echo '!xxx=20' fi if [ ! $xxx == 20 ] then echo '!xxx==20' fi运行结果:
xxx<20 xxx="">5 xxxeq10 xxxne20 ./test.sh: 29: [: 10: unexpected operator xxx=10 ./test.sh: 41: [: 10: unexpected operator ./test.sh: 44: [: 10: unexpected operator xxx!=20 !xxx=20 ./test.sh: 56: [: 10: unexpected operator <!--20-->
参考:
简版:http://zhidao.baidu.com/link?url=WSUZ_h7lRKlWqjRi6fMvcYk0hxg9nWzBfW4xZTLNURF4BjlQWbbM4Wfx6NrcVfL2sa9zu2DO5rQn2kFSvmp0ha
完整版:http://www.jb51.net/article/34332.htm
相关文章推荐
- shell下数字和字符串比较操作命令 if的参数说明
- Shell脚本:时间相关、遍历目录、for、if、字符串比较、数字比较
- shell脚本----if(数字条件,字符串条件,字符串为空)
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
- shell字符串比较、判断是否为数字
- shell下数字和字符串比较 混合比较操作命令
- shell中if条件字符串、数字 [[ ]]和[ ]区别
- shell if 比较
- shell中if比较
- shell脚本--if判断(数字条件、字符串条件)
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
- [Shell] if、for、while流程语句以及整数字符串判断比较的实例详解
- shell中if做比较
- shell中if做比较
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
- shell中if做比较
- shell脚本--if判断(数字条件、字符串条件)
- shell脚本----if(数字条件,字符串条件,字符串为空)
- shell if 小数 比较