shell出现syntax error near unexpected token `<' 解决方法

最新在看一个shell资料时,按照教材,却出现如下错误,不能运行

如下,简单的脚本:

#!/bin/bash
cat |while read line
do
  echo $line
done < <(data.txt|awk '{print $0}')

运行时,却出现如下错误

[root@localhost shellcookbook]# sh while_test.sh 
while_test.sh: line 5: syntax error near unexpected token `<'
while_test.sh: line 5: `done < <(data.txt|awk '{print $0}')'

然后在单行运行时,却没有错误

[root@localhost shellcookbook]# while read line;do echo $line;done < <(cat data.txt|awk '{print $1}')
1
2
3
4

 

在网上查看后,确认无脚步错误,无dos文件错误。还是没有好的解决方法。通过多方求证。原来是bash 3.0后,shell中加入了新的符号"<<<" 可以获取子任务

现将脚本更改如下,成功运行:

#!/bin/bash
cat |while read line
do
  echo $line
done <<< `cat data.txt|awk '{print $0}'`

成功运行

[root@localhost shellcookbook]# sh while_test.sh 
1 2 3 4

 

posted @ 2016-02-22 09:24  Believer007  阅读(24634)  评论(0编辑  收藏  举报