免费注册 查看新帖 |

Chinaunix

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

[文本处理] 关于$() 和 ` `为什么结果不一样 [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之同曦
日期:2017-03-19 09:57:50
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-12 09:14 |只看该作者 |倒序浏览
INPUT_FILE=INPUT_FILE_${BillIndex}_$i                      #BillIndex 和i都是变量

eval INPUT_FILE=`echo \$$INPUT_FILE`

结果 >>>>

改成 eval INPUT_FILE=$(echo \$$INPUT_FILE)
结果 >>>>


$() 和 ` `有什么区别呢?之前一直以为是一样的

论坛徽章:
0
2 [报告]
发表于 2015-11-12 10:05 |只看该作者
本帖最后由 lgfang 于 2015-11-12 10:05 编辑

` ` 更多shell支持,但不如$()好用。后者可以嵌套,不需要额外的转义;而且也不容易写错、看错。好多字体里前者和单引号很难区分。

另外,你的需求不需要用eval + $(),直接用所谓的indirect variable reference就行了

  1. # for bash
  2. name_ref=INPUT_FILE_${BillIndex}_$i
  3. INPUT_FILE=${!name_ref}
  4. # for ksh, man nameref
复制代码
关于indirect variable reference的更多信息: http://lgfang.github.io/computer ... indirect-reference/

论坛徽章:
1
15-16赛季CBA联赛之同曦
日期:2017-03-19 09:57:50
3 [报告]
发表于 2015-11-12 10:32 |只看该作者
回复 2# lgfang

感谢你的方法,不过从我遇到的问题来看,$() 和``除了使用范围之外,应该还有其他差别的

   

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
4 [报告]
发表于 2015-11-12 10:34 |只看该作者
本帖最后由 tc1989tc 于 2015-11-12 10:36 编辑
  1. ...skipping...
  2.               $(command)
  3.        or
  4.               `command`

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

  10.        [color=Red]When  the  old-style  backquote  form of substitution is used, backslash retains its
  11.        literal meaning except when followed by $, `, or \.  The first  backquote  not  pre-
  12.        ceded by a backslash terminates the command substitution.[/color] (请问怎么理解反斜杠保留了他的字面意思啊??) When using the $(command)
  13.        form, all characters between the parentheses make up the command; none  are  treated
  14.        specially.

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

  17.        If the substitution appears within double quotes, word splitting and pathname expan-
  18.        sion are not performed on the results.
复制代码

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
5 [报告]
发表于 2015-11-12 10:36 |只看该作者
...skipping...
              $(command)
       or
              `command`

       Bash performs the expansion by executing command and replacing the command substitu-
       tion 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  pre-
       ceded 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 expan-
       sion are not performed on the results.

论坛徽章:
1
15-16赛季CBA联赛之同曦
日期:2017-03-19 09:57:50
6 [报告]
发表于 2015-11-12 11:06 |只看该作者
回复 5# tc1989tc


    谢谢了先,这个是man 什么可以看到的,英语太水了,能帮忙解释下不,拿去谷歌翻译也没看明白

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
7 [报告]
发表于 2015-11-12 11:25 |只看该作者
应该是 $() 里面的 \ 在第一遍解析展开时不做处理

    $() 里面的所有字符串应该原样传递给sub shell进行解析处理

论坛徽章:
1
15-16赛季CBA联赛之同曦
日期:2017-03-19 09:57:50
8 [报告]
发表于 2015-11-12 21:50 |只看该作者
回复 7# yjh777

感谢分析

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP