成都网站建设设计

将想法与焦点和您一起共享

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://chengdu.cdxwcx.cn/article/djegips.html