shell读取文件三种方法

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的默认值是“”“”“

若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

相关数据

美国怡口舒孚特哪个好?怡可飘和怡口哪个好
日博365官网手机版

美国怡口舒孚特哪个好?怡可飘和怡口哪个好

⌛ 07-25 👁️ 5403
洋山海关业务受理范围一览表, 请收好!(更新)
office365无法登录账号

洋山海关业务受理范围一览表, 请收好!(更新)

⌛ 10-09 👁️ 7436
相公在哪里
日博365官网手机版

相公在哪里

⌛ 07-26 👁️ 5555