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

zhpsam109 2011-11-28 05:36:55
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.


请帮忙举个例子,谢谢!
...全文
226 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
二狗蹲坑 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 ljc007 的回复:]

引用 17 楼 luoyaojun000 的回复:
<< 这不是here 风格么

< 这才是read line < filename 风格

两个<中间有个空格
[/Quote]
嗯嗯!还有就是不知道是不是sh和ksh的问题呢.检查一下.
向良玉 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ljc007 的回复:]

引用 7 楼 zhpsam109 的回复:
注意:
Using Command Output in a Loop

不是文件!

也许是你没有注意到整个背景
人家说的是bash
你就别在ksh里面浪费表情了
[/Quote]
你到bash试试看。。。能做出来就贴出来吧
ljc007 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhpsam109 的回复:]
注意:
Using Command Output in a Loop

不是文件!
[/Quote]
也许是你没有注意到整个背景
人家说的是bash
你就别在ksh里面浪费表情了
ljc007 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 luoyaojun000 的回复:]
<< 这不是here 风格么

< 这才是read line < filename 风格
[/Quote]
两个<中间有个空格
ljc007 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xiangliangyu2008 的回复:]
大哥,我真试不出。。。环境不对?。。。把你的完全copy都执行不了
test.sh: line 5: syntax error near unexpected token `<'
test.sh: line 5: `done < <(ls)'[/Quote]
bash --version
zhpsam109 2011-11-29
  • 打赏
  • 举报
回复
注意:
Using Command Output in a Loop

不是文件!
linus 2011-11-29
  • 打赏
  • 举报
回复
<< 这不是here 风格么

< 这才是read line < filename 风格
向良玉 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ljc007 的回复:]

引用 12 楼 xiangliangyu2008 的回复:
你到bash试试看。。。能做出来就贴出来吧

如果你做不出来,就继续问。

#cat test.sh
#!/bin/bash
while read LINE
do
echo "$LINE"
done < <(ls)

#./test.sh
a.txt
b.txt
test.sh
[/Quote]
大哥,我真试不出。。。环境不对?。。。把你的完全copy都执行不了
test.sh: line 5: syntax error near unexpected token `<'
test.sh: line 5: `done < <(ls)'
向良玉 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 rucypli 的回复:]

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

done<test1.sh
[/Quote]
这个对的。。。
rucypli 2011-11-29
  • 打赏
  • 举报
回复
#!/bin/ksh
while read line
do
echo $line

done<test1.sh
ljc007 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xiangliangyu2008 的回复:]
你到bash试试看。。。能做出来就贴出来吧
[/Quote]
如果你做不出来,就继续问。

#cat test.sh
#!/bin/bash
while read LINE
do
echo "$LINE"
done < <(ls)

#./test.sh
a.txt
b.txt
test.sh
念茜 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljc007 的回复:]

引用 3 楼 zhpsam109 的回复:
他说这种用法,就是要不用管道的:

Assembly code
#!/bin/ksh
while read line
do
echo $line
done < urfile
[/Quote]
还是这个用法见的多,<直接给read
ljc007 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhpsam109 的回复:]
他说这种用法,就是要不用管道的:
[/Quote]
#!/bin/ksh
while read line
do
echo $line
done < urfile
向良玉 2011-11-28
  • 打赏
  • 举报
回复
试了很多。。
明天继续试...
zhpsam109 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.

justkk 2011-11-28
  • 打赏
  • 举报
回复
或者这样
cat ./test1.sh | while read line
..
justkk 2011-11-28
  • 打赏
  • 举报
回复
是不是ksh不支持?
用bash试试

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧