Linux shell 下循环读取文件filename.txt的方式 Bertha 。 2022-12-09 13:56 294阅读 0赞 #!/bin/bash #重定向法;管道法:当遇见管道的时候管道左边的命令的输出会作为管道右边命令的输入然后被输入出来。 printf "*************************************\n" echo " cat file whiel read line" cat test.txt |while read line do echo $line; done # while循环中执行效率最高,最常用的方法。 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变量。 ------------------- #### `for语句效率最高,而在while循环中读写文件时,方法一最为高效` ####
还没有评论,来说两句吧...