重定向的同时结合了heredoc

时间:2015-08-02 07:21:18

标签: bash

尝试以下操作 - 但不起作用:

while read -r cfg val
do
        echo "=$cfg=$val="
done
done < <(sed 's/#.*//;/^ *$/d' << CFGVAL )
cfg1 val1

#some comment
cfg2 #val2
   #cfg3 val3   
CFGVAL

e.g。想

  • while
  • 的输出重定向sed循环的标准输入
  • 并且sed应该从HEREDOC
  • 读取其输入

想要输出

=cf1=val1=
=cfg2==

e.g。想

  • 删除评论#.*
  • 跳过空行

请帮助多重重定向。我知道,有可能用

来解决
while read -r cfg val
do
   #do tests here
   echo "..."
done <<HEREDOC
...
HEREDOC

但寻找第一个解决方案。

1 个答案:

答案 0 :(得分:2)

您的here-doc是流程替换的一部分。这意味着它需要在parens内部。你需要移动近距离:

while read -r cfg val
do
        echo "=$cfg=$val="
done < <(sed 's/#.*//; /^ *$/d' << CFGVAL
cfg1 val1

#some comment
cfg2 #val2
   #cfg3 val3   
CFGVAL
)

这会产生输出:

=cfg1=val1=
=cfg2==
相关问题