Chinaunix

标题: [] and [[]] different?? [打印本页]

作者: super_ebo    时间: 2008-05-18 11:17
标题: [] and [[]] different??
if [ $id < $count ]  ,这里要改为 if [ $id -le $count ]  , -le 是数值比较,而 < 是字符串比较。
          或者 改为 if [[ $id < $count ]] ,虽然这样会执行,但是我发现,它是按照了字符串比较。不是数值比较,运行结果是错的!

我的问题:“为什么 [ $id < $count ] 不能正确执行,而[[ $id < $count ]] 可以正确执行(虽然结果不对)?”[] 和 [[ ]] 的区别??
作者: 寂寞烈火    时间: 2008-05-18 11:29
标题: 回复 #1 super_ebo 的帖子
这类讨论论坛很多请RTFW
作者: waker    时间: 2008-05-19 08:39
[]对儿中 [是个command, ]是个argument
[[]]对儿中 [[与]]都是keyword
作者: walkerxk    时间: 2008-05-19 14:16
原帖由 寂寞烈火 于 2008-5-18 11:29 发表
这类讨论论坛很多请RTFW

纠正一下,是RTFM和STFW
作者: walkerxk    时间: 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.
作者: waker    时间: 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 ]
作者: woodie    时间: 2008-05-19 16:22
楼主,类似问题精华里早有讨论:
请问替代test语句的[ ]和[[ ]]表达式有什么区别啊
作者: super_ebo    时间: 2008-05-19 20:56
谢谢!那边帖子,确实很有帮助1




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2