免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 26189 | 回复: 31

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊  关闭 [复制链接]

论坛徽章:
0
发表于 2004-03-12 08:45 |显示全部楼层
我看有的书说可以用[ 某条件 ]替代 test 某条件,也有的书介绍用[[ 某条件 ]]来替代 test 某条件,我看了一些shell脚本,确实有的脚本用
if [ 某条件 ]; then
  some statement
fi

有的脚本用
if [[ 某条件 ]]; then
  smoe statement
fi

它们之间有什么区别吗?

论坛徽章:
0
发表于 2004-03-12 11:35 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

[[]]支持通配符,

论坛徽章:
0
发表于 2004-03-12 13:01 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

谢谢你的回答,除了这一点外还有别的区别吗?

论坛徽章:
0
发表于 2004-03-29 09:04 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

不同的shell会支持不一样的.
bounse shell 应该不支持[[  ]],你可以试一下看它会不会报错
ksh [[ ]]应该支持

论坛徽章:
0
发表于 2004-03-29 09:28 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

bjmm太强了!!!
佩服你!

论坛徽章:
0
发表于 2004-03-29 10:33 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

bjmm就是厉害!

论坛徽章:
0
发表于 2004-03-29 11:27 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

[quote]原帖由 "zhouhaiming"]谢谢你的回答,除了这一点外还有别的区别吗?[/quote 发表:

做个比较吧 环境:bash/ksh
  1. $a=1 b=2
  2. $[ $a > $b ]&&echo ok||echo error
  3. ok
  4. $[[ $a > $b ]]&&echo ok||echo error
  5. error
复制代码
ps:我个人认为[[...]]是[...]和test命令的增强"版"

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2004-03-29 12:01 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

窃以为:

[[ ]]是shell内部命令,许多shell不支持这种方式。
[]是外部命令,是通用指令。

[] 中不支持 < >判断,应使用 -gt -lt代替,上述指令应为:
[ $a -gt $b ]&&echo ok||echo error

请各大师指正。

论坛徽章:
0
发表于 2004-03-29 20:43 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

[[ 更多用于数字判断
是shell的关键字

[ = test 是shell的内置命令

论坛徽章:
0
发表于 2004-03-29 20:51 |显示全部楼层

请问替代test语句的[ ]和[[ ]]表达式有什么区别啊

ksh对[..]和[[..]]的解释:
(( expression ))
              The arithmetic expression expression is evaluated; equivalent to
              let "expression".  See Arithmetic Expressions and the  let  com-
              mand below.

       [[ expression ]]
              Similar to the test and [ ... ] commands (described later), with
              the following exceptions:
                o    Field splitting and file name  generation  are  not  per-
                     formed on arguments.
                o    The  -a  (and) and -o (or) operators are replaced with &&
                     and ||, respectively.
                o    Operators (e.g., -f, =, !, etc.) must be unquoted.
                o    The second operand of != and = expressions  are  patterns
                     (e.g., the comparison in
                                        [[ foobar = f*r ]]
                     succeeds).
                o    There  are two additional binary operators: < and > which
                     return true if their first string operand is  less  than,
                     or  greater  than,  their  second string operand, respec-
                     tively.
                o    The single argument form of  test,  which  tests  if  the
                     argument  has  non-zero  length,  is not valid - explicit
                     operators must be always be used, e.g., instead of
                                              [ str ]
                     use
                                           [[ -n str ]]
o    Parameter, command and arithmetic substitutions are  per-
                     formed  as  expressions are evaluated and lazy expression
                     evaluation is used for the &&  and  ||  operators.   This
                     means that in the statement
                                  [[ -r foo && $(< foo) = b*r ]]
                     the  $(<  foo)  is  evaluated if and only if the file foo
                     exists and is readable.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP