RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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脚本中按行读取文本文件
URL分享:http://www.jxjierui.cn/article/djegips.html