2014-02-12 129 views
0

我見過太多的文檔,並在bash腳本使用while的例子,但沒有解釋什麼<在此做:使用<在while循環

while read p; do 
echo $p 
done < $filename 

到底是什麼在程序流程以上,以及<如何工作?

+0

http://en.wikipedia.org/wiki/Redirection_(computing)或http://sc.tamu.edu/help/general/unix/redirection.html – anubhava

回答

2

您可能想要參考redirection

說:

command < filename 

執行commandfilename作爲輸入。

您提到的命令每次從$filename表示的文件中讀取一行。

您可能還想要參考help whilehelp read

+0

謝謝。似乎很奇怪,我的示例中的重定向發生在命令「done」 - 這看起來並不直觀 – johnbakers

+0

@OpenLearner這是'read'是_consuming_重定向和'while'導致它發生在循環。 – devnull

+0

整個'while ...;做...; done'循環是'bash'中的單個*複合命令*。輸入重定向適用於它; 'read'命令從包含它的'while'循環中繼承它的標準輸入,這允許你每次在循環中調用'read'時使用不同的輸入行。 – chepner