免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: chliang315
打印 上一主题 下一主题

弱弱的问一下,-gt 和 > 有什么不同呀 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-03-07 10:45 |只看该作者

  1. #!/bin/bash
  2. if [[ 0 ]]; then
  3.   echo A
  4. else
  5.   echo B
  6. fi
复制代码


  1. #!/bin/bash
  2. if [[ 1 ]]; then
  3.   echo A
  4. else
  5.   echo B
  6. fi
复制代码

这两个脚本运行的结果都是
A
为什么呢?

论坛徽章:
0
12 [报告]
发表于 2008-03-07 16:27 |只看该作者
这两种条件应该都是真的

论坛徽章:
0
13 [报告]
发表于 2008-03-07 16:56 |只看该作者

  1. 6.4 Bash Conditional Expressions
  2. ...
  3. string1 < string2
  4.     True if string1 sorts before string2 lexicographically in the current locale.
  5. string1 > string2
  6.     True if string1 sorts after string2 lexicographically in the current locale.
  7. arg1 OP arg2
  8.     OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
复制代码

论坛徽章:
0
14 [报告]
发表于 2008-03-10 11:16 |只看该作者
原帖由 chliang315 于 2008-3-7 09:57 发表
那为什么下面的又可以呀
if [[ $i == 10 ]]
then
{
...
}
if

因为字符串的相等和数字的相等是一样的,但是最好还是使用if [ $i -eq 10 ]

原帖由 prc 于 2008-3-7 10:05 发表
这个跟shell有关吧

在bash上[[ $i -gt 20 ]] 和 [[ $i > 20 ]]都是可以的
但是-gt更典型的用法应该是 [ $i -gt 20 ]
有的shell是不支持 [[ ]]的

你确定你那 [[ 9 > 20 ]]为真?
walkerxk@www:~$ if [[ 9 -gt 20 ]];then echo 1;else echo 0;fi
0
walkerxk@www:~$ if [[ 9 > 20 ]];then echo 1;else echo 0;fi
1
walkerxk@www:~$
原帖由 prc 于 2008-3-7 10:45 发表

#!/bin/bash
if [[ 0 ]]; then
  echo A
else
  echo B
fi



#!/bin/bash
if [[ 1 ]]; then
  echo A
else
  echo B
fi

这两个脚本运行的结果都是
A
为什么呢?

凡是常数,返回值都是true,只有表达式才会有判断真假。

[ 本帖最后由 walkerxk 于 2008-3-10 11:18 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP