日期:2014-05-16  浏览次数:20707 次

awk 命令 标准输出作为数据源问题
test.js 文件内容
viewport.js+touchXY.js+slider.js=temp/new.js
define.js+common.js=temp/c.js

config=$( cat build.config.ini | tr -d ' ' )

命令:echo $config | awk 'NR==1{print $2}'  
输出:define.js+common.js=temp/c.js         //@1

命令:awk 'NR==1{print $2}' build.config.ini  //@2
输出:空

为什么@2 正常输出为空 而@1确不行
标准输出作为数据源在这里面有什么问题吗? 很迷惑

先谢谢了

------解决方案--------------------
引用:
顶顶,等待。。。。


楼主试试 这两种方式print config有什么不同

echo $config
echo "$config"

结果可能比较意外吧,这个结果也可以解释楼主观察到的现象

这个和shell的word splitting或者field splitting有关系,在不带引号的情况下,相当于两个独立的参数, 中间多少个white space并没有关系

好比 ./myProgram a b 和 ./myProgram a           b 对于程序myProgram来说没有任何区别

贴上一段man bash供楼主参考

   Word Splitting
       The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within  double  quotes
       for word splitting.

       The  shell  treats  each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters.  If
       IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at  the  beginning
       and  end  of  the  results  of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to
       delimit words.  If IFS has a value other than the default, then sequences of the whitespace characters space and  tab  are  ignored  at  the
       beginning  and  end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character).  Any character in
       IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field.  A sequence of IFS  whitespace  charac‐
       ters is also treated as a delimiter.  If the value of IFS is null, no word splitting occurs.

       Explicit  null&