免费注册 查看新帖 |

Chinaunix

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

[其他] 反引号的问题 [复制链接]

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-13 12:15 |只看该作者 |倒序浏览
本帖最后由 代号:军刀 于 2013-04-13 12:19 编辑

process=`netstat -tunlp | awk '/mysql/{split($4,a,":");if(a[2] == '$new_port')print gensub(/(^[0-9]+)\/mysqld/,"\\1",1,$7)}'`
加了反引号后变错
  1. [root@ ~]$ netstat -tunlp | awk '/nginx/{split($4,a,":");if(a[2] == "80")print gensub(/(^[0-9]+)\/nginx/,"\\1",1,$7)}'
  2. 1486
复制代码
[root@ ~]$ sh -x test.sh
++ netstat -tunlp
++ awk '/nginx/{split($4,a,":");if(a[2] == "80")print gensub(/(^[0-9]+)\/nginx/,"\1",1,$7)}'
+ process=$'\001'

很明显,错误的地方在"\\1"变成了"\1",不知道为什么

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
2 [报告]
发表于 2013-04-13 12:40 |只看该作者
本帖最后由 reyleon 于 2013-04-13 12:43 编辑

还真是,把 `` 改成 $() 好了,不知道为什么,等大神解答吧

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
3 [报告]
发表于 2013-04-13 12:46 |只看该作者
本帖最后由 代号:军刀 于 2013-04-13 12:49 编辑

回复 2# reyleon


      我也纳闷呢

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
4 [报告]
发表于 2013-04-13 12:54 |只看该作者
或许本身 `` 就对于双反斜杠\\就有问题的,abs 287页 有简单的例子..
  1.        
  2. The $(...) form has superseded backticks for command substitution.

  3. output=$(sed -n /"$1"/p $file)   # From "grp.sh"        example.
  4.              
  5. # Setting a variable to the contents of a text file.
  6. File_contents1=$(cat $file1)      
  7. File_contents2=$(<$file2)        # Bash permits this also.

  8. The $(...) form of command substitution treats a double backslash in a different way than `...`.

  9. bash$ echo `echo \\`


  10. bash$ echo $(echo \\)
  11. \
  12.              

  13. The $(...) form of command substitution permits nesting. [3]

  14. word_count=$( wc -w $(echo * | awk '{print $8}') )
  15. Or, for something a bit more elaborate . . .
复制代码

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
5 [报告]
发表于 2013-04-13 13:09 |只看该作者
   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 new-
       lines 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 back-
       quote 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.


命令替换用命令的输出取代命令本身,它的形式可以是下面任意一种:
$( 命令)
或者
`命令`
进行扩展时,Bash先执行命令,并把该命令的标准输出中最后面的换行符删除,用结果取代命令替换。这
个时候中间的换行符不删除,但是它们可能在单词拆分时被删除。命令替换形式$(cat 文件名) 可以用与
其等价但速度更快的$(< 文件名) 来取代。
如果使用了旧式的反引号,反斜杠就保留其字面含义,除非它后面是"$"、"`"或者"\"。第一个不是出
现在反斜杠后面的反引号结束命令替换。如果使用了$( 命令) 的形式,则括号中间的所有字符组成命令,
没有任何一个会被特殊处理。命令替换可以嵌套。使用反引号形式进行嵌套时,里面的反引号需要用反斜杠
转义。如果命令替换出现在双引号之间,则其结果不会进行单词拆分和文件名扩展。


论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
6 [报告]
发表于 2013-04-13 13:37 |只看该作者
回复 5# reyleon


    学习了,When  the  old-style  backquote form of substitution is used, backslash retains its literal meaning except when followed by $, ‘, or \.
    要想上面那反引号内的命令输出正常,就得把"\\1"给改成"\\\1"
   既然有old-style backquote,不知new-style backquote又会是怎样呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP