+ -
当前位置:首页 → 问答吧 → 精通unix shell脚本编程中关于while循环的问题!

精通unix shell脚本编程中关于while循环的问题!

时间:2011-11-28

来源:互联网

while read LINE
do
echo "$LINE"
done < <(command)

I know this looks a bit odd. I got this trick from one of my co-workers, Brian Beers.
What we are doing here is input redirection from the bottom of the loop, after the
done loop terminator, specified by the done < notation. The < (command) notation
executes the command and points the command’s output into the bottom of the loop.
NOTE The space between<<in < <(command)is required!

我测试了一下,始终报错,如下:

#!/bin/ksh
while read line
do
echo $line

done< <(cat ./test1.sh)

test2.sh[2]: 0403-057 Syntax error at line 6 : `<' is not expected.


请帮忙举个例子,谢谢!

作者: zhpsam109   发布时间: 2011-11-28

是不是ksh不支持?
用bash试试

作者: justkk   发布时间: 2011-11-28

或者这样
cat ./test1.sh | while read line
..

作者: justkk   发布时间: 2011-11-28

他说这种用法,就是要不用管道的:

Using Command Output in a Loop
The technique shown here is a nice little trick to execute a command and use the
command’s output in a loop without using a pipe:

while read LINE
do
echo "$LINE"
done < <(command)
I know this looks a bit odd. I got this trick from one of my co-workers, Brian Beers.
What we are doing here is input redirection from the bottom of the loop, after the
done loop terminator, specified by the done < notation. The < (command) notation
executes the command and points the command’s output into the bottom of the loop.

作者: zhpsam109   发布时间: 2011-11-28