将curlredirect到while循环

有没有办法将curl输出redirect到while循环?

while read l; do echo 123 $l; done < curl 'URL' 

还是有更好的方法来做到这一点? 我只需要阅读一个页面的内容,并在每一行添加一些内容并保存到一个文件中。

您将需要使用进程替换重定向curl的输出,如下所示:

 while read -rl; do echo "123 $l" done < <(curl 'URL') 

您还可以使用带引号的命令替换herestring的输出,如下所示:

 while read -rl; do echo "123 $l" done <<<"$(curl 'URL')" 

(尽管过程替代是优选的)

注意:为了重定向到一个文件,你可以重定向块的输出,而不是一次一行:

 :>outfile ## truncate outfile if it exists { while read -rl; do echo "123 $l" done < <(curl 'URL') }>outfile 

您可以使用管道运算符|

 curl 'URL' | while read l; do echo 123 $l >> file.txt done