逐行讀取文件的Shell代碼

gcd8 9年前發布 | 2K 次閱讀 Shell

linux shell 按行循環讀入文件方法

#!/bin/bash

printf "*\n" echo " cat file while read line" cat one.log |while read line do echo $line; done

printf "*\n" echo "while read line <file" while read line do echo $line; done <one.log

printf "*\n" echo "for line in cat test.txt" SAVEIFS=$IFS IFS=$(echo -en "\n") for line in $(cat test.txt) do echo $line; done IFS=$SAVEIFS</pre>

前二種方法可以分別簡化成一行簡短的代碼

cat one.log|while read line;do echo $line;done

while read line; do echo $line; done<one.log</pre>

 本文由用戶 gcd8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!