2016-04-22 45 views
0

我有一个test.sh文件,它通过一个文本文件test.bed awks:如何用sh来迭代awk的每个输出?

1 31 431 
1 14 1234 
1 54 134 
2 435 314 
2 314 414 

while read line;do 
    echo $line 
    # ... more code ... # 
done < <(awk -F '\t' '$1 == 1 {print $0}' test.bed) 

bash test.sh工程运行此,但sh test.sh它提供了语法错误。我如何使用sh而不是bash来完成上述操作?


,它给人的错误是

test.sh: line 5: syntax error near unexpected token `<' 
test.sh: line 5: `done < <(awk -F '\t' '$1 == 1 {print $0}' test.bed)' 
+0

什么是错误? – vidit

+0

@vidit test.sh:第5行:意外标记附近的语法错误'<' test.sh:第5行:'done <(awk -F'\ t''$ 1 == 1 {print $ 0}'test。床)' –

+0

你正在使用的env?我没有经历过这种错误。你能否粘贴你的完整脚本? – ClaudioM

回答

2

Process Substitutionbash扩展 - 不是POSIX规定。这是sh兼容..

#!/bin/sh 

awk -F '\t' '$1 == 1 {print $0}' test.bed | while read line; do 
echo $line 
    # ... more code ... # 
done 
+0

这充满了bug,它会咬你给test.bed,你运行工具的目录,环境设置等各种组合。它也有冗余的awk代码,但这并不重要。 ** IF **这样的shell循环是必需的,那么编写它的方法将是'awk -F'\ t''$ 1 == 1'test.bed |而IFS =读取-r行;做 echo“$ line”; done'。如果您的shell支持它以增强健壮性,请使用'printf'而不是'echo'。 –

+0

不,我说这种方法是错误的,即使方法正确,您的答案仍然是实施它的错误方法。看到我的第一条评论。 –

+0

如果以'#!/ bin/bash'开头,原始脚本将会起作用。我尽量避免使用脚本,但没有原始海报的必要。 – Eric