for i in cat file 和 while read line的区别

#!/bin/bash
#Auth: andy
#Date: 20191114
#Describe: 统计

word_file="./cui"

###会把每行的单词做为新的每一行的输出
#for i in `cat ./cui`;do
#       l=`echo $i|wc -L`
#       #echo ${l}
#       if [ ${l} -gt 6 ];then
#               echo $i
#       fi
#done

##单词还是在本行里面
a=0
while read line
do
        a=$[a+1]
        echo "第${a} 行内容"
        for i in $line;
        do
                l=`echo $i|wc -L`
                if [ ${l} -gt 6 ];then
                echo -n $i
                fi
        done
        echo 
done < ./cui

while读取是按照shell read 分割符
而 IFS 是一种 set 变量,当 shell 处理"命令替换"和"参数替换"时,shell 根据 IFS 的值,默认是 space, tab, newline 来拆解读入的变量,然后对特殊字符进行处理,最后重新组合赋值给该变量。
shell while 读取行_第1张图片

     直接输出IFS是看不到的,把它转化为二进制就可以看到了,"040"是空格,"011"是Tab,"012"是换行符"\n" 。最后一个 012 是因为 echo 默认是会换行的。

     更改IFS

$ cat 1.txt
1,a
2,b
3,c
4,d
$ cat test.sh
shell while 读取行_第2张图片
采用逗号分割