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


http://blog.csdn.net/hittata/article/details/7042779

  1. #/bin/bash  
  2. printf "*************************************\n"  
  3. echo " cat file whiel read line"  
  4. cat test.txt |while read line  
  5. do  
  6.   echo $line;  
  7. done  
  8. printf "*************************************\n"  
  9. echo "while read line <file"  
  10. while read line  
  11. do  
  12.   echo $line;  
  13. done <test.txt
  14. printf "*************************************\n"  
  15. echo "for line in cat test.txt"  
  16. SAVEIFS=$IFS  
  17. IFS=$(echo -en "\n")  
  18. for line in $(cat test.txt)  
  19. do  
  20.   echo  $line;  
  21. done  
  22. IFS=$SAVEIFS 

注意:for line in $(cat test.txt)   當文件中有空格或者tab 時,一定要設置一下IFS變量。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM