for i in cat file
和 while read line的区别shell
#!/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 来拆解读入的变量,而后对特殊字符进行处理,最后从新组合赋值给该变量。bash
直接输出IFS是看不到的,把它转化为二进制就能够看到了,"040"是空格,"011"是Tab,"012"是换行符"\n" 。最后一个 012 是由于 echo 默认是会换行的。 更改IFS
$ cat 1.txt
1,a
2,b
3,c
4,d
$ cat test.sh
采用逗号分割ide