|
发表于 2008-5-25 00:38:02
|
显示全部楼层
首先声明我不是高手,看到你只发了几个帖子,为了不至于对这个论坛太失望,说一点我的看法。
你没有明确说明是用的哪种shell,这里就姑且认为是bash吧。
对于echo 'aaa////aaa' | sed 's/\\//\\\\\\//g' 有错误,是因为bash对单引号中的每个字符都保持原来得意思,不会对\特别处理,所以再单引号中是不能包括单引号的,比如'\''这样就是错误的,这里\并没有特殊的意思。关于单引号的用法,在bash的手册中是这样说的:
Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
所以sed接收到的参数就是s/\\//\\\\\\//g,所以最后的那个/就是多余的了,所以是错误的。
对于x=`echo 'aaa////aaa' | sed 's/\\//\\\\\\//g'`;echo $xx
反引号是命令替换的意思,在bash手册中是如下介绍的:
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.
所以在执行命令前被替换成了,echo 'aaa////aaa' | sed 's/\//\\\//g'
sed得到的参数就是s/\//\\\//g,所以就是正确的。
对于$(),上面的英文解释里也包括了,在执行命令替换前不会把\\替换为\,所以把第二个的`改为$()发生了错误。
对于sed,在命令中包括/时,最好是改变定界符。
提醒一下,最后两个命令的结果是不同的。 |
|