linux shell 按行循环读入文件方法

时间:2023-01-30 07:12:25

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变量。