shell脚本中按行读取文本文件

shell是外壳的意思,就是操作系统的外壳。我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包括ls、cd、pwd等等。总结来说,Shell是一个命令解释器,它通过接受用户输入的Shell命令来启动、暂停、停止程序的运行或对计算机进行控制。

假设读取的文件为当期目录下的 test.txt 文件,内容如下:

Google
Runoob
Taobao

实例 1

#!/bin/bash 
while read line
do
   echo $line
done 

执行输出结果为:

Google
Runoob
Taobao

实例 2

#!/bin/bash 
cat test.txt | while read line
do
   echo $line
done

执行输出结果为:

Google
Runoob
Taobao

实例 3

for line in `cat  test.txt`
do
   echo $line
done

执行输出结果为:

Google
Runoob
Taobao

for 逐行读和 while 逐行读是有区别的,如:

$ cat test.txt
Google
Runoob
Taobao
$ cat test.txt | while read line; do echo $line; done
Google
Runoob
Taobao
$ for line in $(
  
   do 
   echo 
   $line; 
   done Google Runoob Taobao 
  

当前名称:shell脚本中按行读取文本文件
本文来源:http://www.csdahua.cn/qtweb/news18/333868.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网