免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2051 | 回复: 7
打印 上一主题 下一主题

[] and [[]] different?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-18 11:17 |只看该作者 |倒序浏览
if [ $id < $count ]  ,这里要改为 if [ $id -le $count ]  , -le 是数值比较,而 < 是字符串比较。
          或者 改为 if [[ $id < $count ]] ,虽然这样会执行,但是我发现,它是按照了字符串比较。不是数值比较,运行结果是错的!

我的问题:“为什么 [ $id < $count ] 不能正确执行,而[[ $id < $count ]] 可以正确执行(虽然结果不对)?”[] 和 [[ ]] 的区别??

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2008-05-18 11:29 |只看该作者

回复 #1 super_ebo 的帖子

这类讨论论坛很多请RTFW

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
3 [报告]
发表于 2008-05-19 08:39 |只看该作者
[]对儿中 [是个command, ]是个argument
[[]]对儿中 [[与]]都是keyword

论坛徽章:
0
4 [报告]
发表于 2008-05-19 14:16 |只看该作者
原帖由 寂寞烈火 于 2008-5-18 11:29 发表
这类讨论论坛很多请RTFW

纠正一下,是RTFM和STFW

论坛徽章:
0
5 [报告]
发表于 2008-05-19 14:24 |只看该作者
walkerxk@www:~$ if [ 9 -gt 10 ];then echo 0;else echo 1;fi
1
walkerxk@www:~$ if [[ 9 -gt 10 ]];then echo 0;else echo 1;fi
1
walkerxk@www:~$ if [[ 9 > 10 ]];then echo 0;else echo 1;fi
0
walkerxk@www:~$ if [ 9 > 10 ];then echo 0;else echo 1;fi
0
walkerxk@www:~$
我这[ $id < $count ]可以,估计是容错了。
[ expr ]
              Return  a  status  of  0  or 1 depending on the evaluation of the conditional expression expr.
              Each operator and operand must be a separate argument.  Expressions are composed of  the  pri‐
              maries  described  above under CONDITIONAL EXPRESSIONS.  test does not accept any options, nor
              does it accept and ignore an argument of -- as signifying the end of options.
[[ expression ]]
              Return  a  status  of 0 or 1 depending on the evaluation of the conditional expression expres‐
              sion.  Expressions are composed of the primaries described  below  under  CONDITIONAL  EXPRES‐
              SIONS.   Word  splitting  and pathname expansion are not performed on the words between the [[
              and ]]; tilde expansion, parameter  and  variable  expansion,  arithmetic  expansion,  command
              substitution,  process  substitution,  and quote removal are performed.  Conditional operators
              such as -f must be unquoted to be recognized as primaries.

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
6 [报告]
发表于 2008-05-19 14:40 |只看该作者

回复 #5 walkerxk 的帖子

try

  1. echo 123 >10
  2. cat 10
  3. [ 9 > 10 ]
  4. cat 10
复制代码



[ 9 > 10 ]
的意思是    1>10 [ 9 ]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
7 [报告]
发表于 2008-05-19 16:22 |只看该作者
楼主,类似问题精华里早有讨论:
请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

论坛徽章:
0
8 [报告]
发表于 2008-05-19 20:56 |只看该作者
谢谢!那边帖子,确实很有帮助1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP