shell 里 使用while 循环行读取文件

使 用while 循环逐行读取文件内容



使用 set 命令查看当前shell变量

linux 里 分隔符保存在 变量 "IFS" 里,默认的分隔符空格"" ; 制表符 \t ; 换行符 \n

[root@host103 test]# set |grep IFS

image-20210716083856595



使用for 循环读取文件内容

当文件中有空格时,直接使用for 循环就会分隔开内容

image-20210716084222119

image-20210716084331761


使用while read ,行读取文件

使用while read 命令读取文件,目标文件内容以 重定向输入的形式输入

方法一

[root@host103 test]# while read  line    
> do
> echo "$line"
> done < /test/users.txt

image-20210719082405415



方法二

[root@host103 test]# cat users.txt  | while read line
> do
> echo "$line"
> do

image-20210719083140861

posted @ 2021-07-19 08:36  知己一语  阅读(3602)  评论(0编辑  收藏  举报