for_null.sh内容:
1 #!/bin/bash 2 #for null line 3 #version 1.0 by feng 4 5 IFS=$'\n' 6 for i in `cat $1` 7 do 8 if [ ${#i} -eq 0 ];then 9 echo "the line is null" 10 else 11 echo "$i" 12 fi 13 done
1 [root@localhost for]# cat a.txt 2 feng 123 3 4 xiao 456 5 6 7 [root@localhost for]# cat for_null.sh
[root@localhost for]# cat -A -n a.txt
1 feng 123$
2 $
3 xiao 456$
4 $
5 ^I^I $
1 [root@localhost for]# sh for_null.sh a.txt 2 feng 123 3 xiao 456 4 5 6 [root@localhost for]#
for小结:
for循环变量列表取值特点:当以换行符作为分隔符时,此时的变量取值列表为:“feng 123” "xiao 456" 换行符 " "(中间为空格) " "(中间为tab键);默认跳过换行符。
while_null.sh内容:
[root@centos17 shell]# cat while_null.sh #!/bin/bash #for null line #version 1.0 by feng while read line do if [ ${#line} -eq 0 ];then continue; fi echo $line done < a.txt [root@centos17 shell]# [root@centos17 shell]# sh while_null.sh feng 123456 xiao 445566 [root@centos17 shell]# [root@centos17 shell]# cat -A a.txt feng 123$ $ xiao 456$ $ ^I^I$ [root@centos17 shell]#
while小结:
while循环跳过空行特点:while默认以换行符作为分隔符,缺点是不跳过空行。通过判断,指定while跳过空行时,while会跳过空行、空格行及tab键所在的所有行。
总结:
for循环指定换行符作为分隔符时,默认只跳过空行,空格行和tab键所在的行正常输出;
while循环以换行符作为分隔符,跳过空行时,会跳过空行,空格行及tab键所在的行。
为者常成,行者常至
Give me five~!