shell读取文件三种方法

Shell按行读取文件的3种方法

Shell按行读取文件的方法有很多,常见的三种方法如下:

定义脚本路径的方法

SHELL_SCRIPTS="$(dirname "${BASH_SOURCE[0]}")"
cur_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

要读取的文件:

1 [root@mini05 20180930-2]# cat file.info 
2 111
3 222
4 333 444
5 555 666

写法一:

 1 [root@mini05 20180930-2]# cat read1.sh 
 2 #!/bin/bash
 3 ################ Version Info ##################
 4 # Create Date: 2018-09-29
 5 # Author:      zhang
 6 # Mail:        zhang@xxx.com
 7 # Version:     1.0
 8 # Attention:   按行读取文件
 9 ################################################
10 
11 # 加载环境变量
12 . /etc/profile
13 . ~/.bash_profile
14 . /etc/bashrc
15 
16 # 脚本所在目录及脚本名称
17 script_dir=$( cd "$( dirname "$0"  )" && pwd )
18 script_name=$(basename ${0})
19 
20 exec < ${script_dir}/file.info
21 while read line; do
22   echo "${line}"
23 done

写法二:

 1 [root@mini05 20180930-2]# cat read2.sh 
 2 #!/bin/bash
 3 ################ Version Info ##################
 4 # Create Date: 2018-09-29
 5 # Author:      zhang
 6 # Mail:        zhang@xxx.com
 7 # Version:     1.0
 8 # Attention:   按行读取文件
 9 ################################################
10 
11 # 加载环境变量
12 . /etc/profile
13 . ~/.bash_profile
14 . /etc/bashrc
15 
16 # 脚本所在目录及脚本名称
17 script_dir=$( cd "$( dirname "$0"  )" && pwd )
18 script_name=$(basename ${0})
19 
20 cat ${script_dir}/file.info | while read line;do
21   echo "${line}"
22 done

写法三

 1 [root@mini05 20180930-2]# cat read3.sh 
 2 #!/bin/bash
 3 ################ Version Info ##################
 4 # Create Date: 2018-09-29
 5 # Author:      zhang
 6 # Mail:        zhang@xxx.com
 7 # Version:     1.0
 8 # Attention:   按行读取文件
 9 ################################################
10 
11 # 加载环境变量
12 . /etc/profile
13 . ~/.bash_profile
14 . /etc/bashrc
15 
16 # 脚本所在目录及脚本名称
17 script_dir=$( cd "$( dirname "$0"  )" && pwd )
18 script_name=$(basename ${0})
19 
20 while read line; do
21   echo "${line}"
22 done < ${script_dir}/file.info

从文件中读取内容的方法有两种:

第一种:在for循环中使用cat 来读取文件的内容;
第二种:在while循环中使用read命令,通过文件描述符一行一行的读取文件内容。

二、准备文件

创建test.log文件,保存相应的数据,查看内容如下:

jdbk@jdbkdeMacBook-Pro xsjq % cat test.log 
line1 aaa
line2 bbb
line3 ccc
1
2
3
4

三、使用for循环
方式一、

#!/bin/bash
IFS=$'\n'
for line in `cat filename`
do
        echo $line
done
1
2
3
4
5
6

方式二、

#!/bin/bash
IFS=$'\n'
for line in $(cat filename)
do
 echo $line
done
1
2
3
4
5
6

使用for循环从文件中读取内容时,默认情况下是以空格作为分隔符来读取文件内容;

因为for循环以环境变量IFS的值作为分隔符,而IFS的默认值是“<space/空格>”“<tab/制表符>”“<newline/新行>”
若IFS字段值未设置,则默认以空格作为分隔符来读取文件内容;
若IFS字段值设为“\n”,则以行作为分隔符来读取文件内容;
若IFS字段值设为“\t”,则以制表符作为分隔符来读取文件内容。

运行脚本文件 for.sh,结果:以作为分隔符来读取文件内容

jdbk@jdbkdeMacBook-Pro xsjq % sh for.sh 
line1 aaa
line2 bbb
line3 ccc
1
2
3
4

若想以空格作为分隔符来读取文件内容,只需去掉对IFS的赋值,即可~
运行去除对IFS的赋值的脚本文件 for.sh,结果:以空格作为分隔符来读取文件内容

jdbk@jdbkdeMacBook-Pro xsjq % cat for.sh 
#!/bin/bash
for line in `cat test.log`
do
        echo $line
done
jdbk@jdbkdeMacBook-Pro xsjq % sh for.sh 
line1
aaa
line2
bbb
line3
ccc
1
2
3
4
5
6
7
8
9
10
11
12
13

四、使用while循环
方式一、

while read -r line
do
 echo $line
done < filename
1
2
3
4

方式二、

cat filename | while read -r line
do
    处理逻辑
done
1
2
3
4

方式三、

exec < filename
while read -r line
do
    处理逻辑
done
1
2
3
4
5

While循环中read命令从标准输入中读取一行内容并保存到变量中;
-r选项保证读入的内容是原始的内容,意味着反斜杠转义的状况不会出现;
输入重定向操作符“< ” 是打开并读取文件filename,然后将它作为read命令的标准输入。

运行结果:

jdbk@jdbkdeMacBook-Pro xsjq % cat while1.sh 
#!/bin/bash
while read -r line
do
    echo $line
done < test.log

jdbk@jdbkdeMacBook-Pro xsjq % cat while2.sh 
#!/bin/bash
cat test.log | while read -r line
do
    echo $line
done

jdbk@jdbkdeMacBook-Pro xsjq % cat while3.sh
#!/bin/bash
exec < test.log
while read -r line
do
    echo $line
done

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

推荐阅读更多精彩内容