Shell while循环按行读文件

方式1:使用cat读取文件内容,然后通过管道进入while循环处理

     cat FILE_PATH|while read line
        do
            cmd
        done
eg:
[root@test01 ~]# cat read.sh
#!/bin/bash
cat sjx.sh|while read line
do
  echo $line
done

方式2: 通过文件重定向方式输入到while 语句中

        while read line
        do
            cmd
        done<FILE
[root@test01~]# cat while.sh 
while read line
do
    echo $line
done <"/sjx.sh"    <<===这里使用绝对路径,或者在脚本定义文件路径

方式3:采用exec读取文件,然后进入while循环处理。

        exec </path/file
        while read line
        do
            cmd 
        done
# 这个方式实际用的频率相对较前面两种方式较少

以上三种为日常使用的读取文件方式,个人喜好用重定向方法,但具体使用哪种需要根据实际情况结合,没有特别规定。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容