免费注册 查看新帖 |

Chinaunix

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

大家来说说(),(()),【】,【【】】的作用和区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-11 07:49 |只看该作者 |倒序浏览

论坛徽章:
0
2 [报告]
发表于 2008-07-11 08:05 |只看该作者

回复 #1 爱知 的帖子

[[]]是比[]更通用的一种结构,它返回一个退出码,(())是c的扩展,如for ((a=0,a<=12,a++))

论坛徽章:
0
3 [报告]
发表于 2008-07-11 08:10 |只看该作者
[[  ]]  中表达不太一样,而 [ ] 中的 < > 需要用转义 \< \>,如果有多个表达式,在[[ ]] 中用 && || 来组合,而[ ] 中是用 -a -o 来组合
(( )) 中,所有的变量(加不加$无所谓)都是数值。

$() 是子命令
()是子进程

[ 本帖最后由 springwind426 于 2008-7-11 08:30 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-07-11 08:16 |只看该作者
()     在一子shell(sub-shell)中运行括号括起来的命令
(())   在某个shell中对变量进行求值和赋值,并进行数学计算
$(())  对括起来的表达式进行求值
[]     与test命令相同
[[]]   用于字符串比较
$()    命令替换
``     命令替换
(())常用于算术运算比较,
[[]]常用于字符串的比较,

以前有人问过

论坛徽章:
0
5 [报告]
发表于 2008-07-11 08:26 |只看该作者
楼上的更详细了!顶

论坛徽章:
0
6 [报告]
发表于 2008-07-11 08:36 |只看该作者
原帖由 7717060 于 2008-7-11 08:16 发表
()     在一子shell(sub-shell)中运行括号括起来的命令
(())   在某个shell中对变量进行求值和赋值,并进行数学计算
$(())  对括起来的表达式进行求值
[]     与test命令相同
[[]]   用于字符串比较
$()   ...



[[  ]]  就是条件表达式,在bash中,字符串比较用  > < != == <= >= 只是在 [ ] 中 < >需要转义;对于数值比较,用 -lt -le -eq -ge -gt 来比较

man bash中有这些:
    Command Substitution
       Command substitution allows the output of a command to replace the command name.  There are two forms:

              $(command)
       or
              `command`

       Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing
       newlines  deleted.  Embedded newlines are not deleted, but they may be removed during word splitting.  The command substitution $(cat file) can be
       replaced by the equivalent but faster $(< file).

       When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or  \.   The  first
       backquote not preceded by a backslash terminates the command substitution.  When using the $(command) form, all characters between the parentheses
       make up the command; none are treated specially.

       Command substitutions may be nested.  To nest when using the backquoted form, escape the inner backquotes with backslashes.

       If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.

   Arithmetic Expansion
       Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result.  The format  for  arithmetic  expansion
       is:

              $((expression))

       The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

       The  expression  is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially.  All tokens in
       the expression undergo parameter expansion, string expansion, command substitution, and quote removal.  Arithmetic expansions may be nested.

       The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION.  If expression is  invalid,  bash  prints  a  message
       indicating failure and no substitution occurs.

   Process Substitution
       Process  substitution  is  supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.  It takes the form of
       <(list) or >(list).  The process list is run with its input or output connected to a FIFO or some file in /dev/fd.   The  name  of  this  file  is
       passed  as an argument to the current command as the result of the expansion.  If the >(list) form is used, writing to the file will provide input
       for list.  If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.

       When available, process substitution is performed simultaneously with parameter and  variable  expansion,  command  substitution,  and  arithmetic
       expansion.

论坛徽章:
0
7 [报告]
发表于 2008-07-11 08:37 |只看该作者
有精华贴讨论过

论坛徽章:
0
8 [报告]
发表于 2008-07-11 08:58 |只看该作者
LZ想知道的话其实可以man bash,然后在里面看看。

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

论坛徽章:
0
10 [报告]
发表于 2008-07-11 12:23 |只看该作者
() 命令组.在括号中的命令列表, 将会作为一个子shell来运行.在括号中的变量,由于是在子shell中,所以对于脚本剩下的部分是不可用的. 父进程, 也就是脚本本身, 将不能够读取在子进程中创建的变量, 也就是在子shell中创建的变量.
   初始化数组.

[ ]条件测试.条件测试表达式放在[ ]中. 值得注意的是[是shell内建test命令的一部分, 并不是/usr/bin/test中的外部命令的一个链接.

具体可看:http://doc.linuxpk.com/doc/abs/special-chars.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP