1. 程式人生 > >shell指令碼中的讀檔案(while read line)與寫檔案(here document)

shell指令碼中的讀檔案(while read line)與寫檔案(here document)

shell指令碼中如何讀取外部檔案並進行相關操作呢?正規化如下,是要背滴:

while read line;
do
    process $line
done < file

如何在寫中將內容寫入新檔案呢,要用到here document,一種特殊的重定向技術,小范式如下,也是要牢記在心的:

cat << EOF > output.txt
echo "hello"
echo "world"
EOF

這時我們得到了output.txt檔案裡面的內容是

echo "hello"
echo "world"