Shell按行读取文件的3种方法
生活随笔
收集整理的這篇文章主要介紹了
Shell按行读取文件的3种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
Shell按行讀取文件的方法有很多,常見的三種方法如下:
?
要讀取的文件:
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?
轉載于:https://www.cnblogs.com/zhanglianghhh/p/9732532.html
總結
以上是生活随笔為你收集整理的Shell按行读取文件的3种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下网络socket编程——实现
- 下一篇: Linux su切换用户后命令提示符变为