linux shell脚本读取文件时,for循环和while循环的区别

您所在的位置:网站首页 shell脚本while循环读取文件 linux shell脚本读取文件时,for循环和while循环的区别

linux shell脚本读取文件时,for循环和while循环的区别

2023-08-27 08:20| 来源: 网络整理| 查看: 265

按行读取文件是经常遇到的情况,使用 for in `cat $文件名` 和 `cat $文件名`|while read line是有细微区别的,多说无益,上代码

[user@clusternoded1]$ cat test.txt 2020-02-05T00:00:08+0800    C5qmg44mhk9bcSiMzj    245    55340    175    41141 2020-02-05T00:00:14+0800    C1mZNP27x2HXnqVsTf    131    60179    175    1433 [user@clusternoded1]$ cat -A test.txt 2020-02-05T00:00:08+0800^IC5qmg44mhk9bcSiMzj^I245^I55340^I175^I41141$ 2020-02-05T00:00:14+0800^IC1mZNP27x2HXnqVsTf^I131^I60179^I175^I1433$

在这里要注意不可见字符的情况

[user@clusternoded1]$ cat testreadfile.sh  #!/bin/bash for metaline in `cat test.txt` do      echo $metaline done while read LINE do     echo $LINE done < ./test.txt cat ./test.txt|while read LINE do     echo $LINE done [user@clusternoded1]$ ./testreadfile.sh  2020-02-05T00:00:08+0800 C5qmg44mhk9bcSiMzj 245 55340 175 41141 2020-02-05T00:00:14+0800 C1mZNP27x2HXnqVsTf 131 60179 175 1433 2020-02-05T00:00:08+0800 C5qmg44mhk9bcSiMzj 245 55340 175 41141 2020-02-05T00:00:14+0800 C1mZNP27x2HXnqVsTf 131 60179 175 1433 2020-02-05T00:00:08+0800 C5qmg44mhk9bcSiMzj 245 55340 175 41141 2020-02-05T00:00:14+0800 C1mZNP27x2HXnqVsTf 131 60179 175 1433

从上面的运行结果,可以很明显地看出两者的区别。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3