linux shell 按行循环读入文件方法
转http://blog.csdn.net/hittata/article/details/7042779
- #/bin/bash
- printf "*************************************\n"
- echo " cat file whiel read line"
- cat test.txt |while read line
- do
- echo $line;
- done
- printf "*************************************\n"
- echo "while read line <file"
- while read line
- do
- echo $line;
- done <test.txt
- 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
注意:for line in $(cat test.txt) 当文件中有空格或者tab 时,一定要设置一下IFS变量。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。