Shell按行读取文件的方法有很多,常见的三种方法如下:

要读取的文件:

 [root@mini05 -]# cat file.info 

写法一:

 [root@mini05 -]# cat read1.sh
#!/bin/bash
################ Version Info ##################
# Create Date: --
# Author: zhang
# Mail: zhang@xxx.com
# Version: 1.0
# Attention: 按行读取文件
################################################ # 加载环境变量
. /etc/profile
. ~/.bash_profile
. /etc/bashrc # 脚本所在目录及脚本名称
script_dir=$( cd "$( dirname "$" )" && pwd )
script_name=$(basename ${}) exec < ${script_dir}/file.info
while read line; do
echo "${line}"
done

写法二:

 [root@mini05 -]# cat read2.sh
#!/bin/bash
################ Version Info ##################
# Create Date: --
# Author: zhang
# Mail: zhang@xxx.com
# Version: 1.0
# Attention: 按行读取文件
################################################ # 加载环境变量
. /etc/profile
. ~/.bash_profile
. /etc/bashrc # 脚本所在目录及脚本名称
script_dir=$( cd "$( dirname "$" )" && pwd )
script_name=$(basename ${}) cat ${script_dir}/file.info | while read line;do
echo "${line}"
done

写法三:

 [root@mini05 -]# cat read3.sh
#!/bin/bash
################ Version Info ##################
# Create Date: --
# Author: zhang
# Mail: zhang@xxx.com
# Version: 1.0
# Attention: 按行读取文件
################################################ # 加载环境变量
. /etc/profile
. ~/.bash_profile
. /etc/bashrc # 脚本所在目录及脚本名称
script_dir=$( cd "$( dirname "$" )" && pwd )
script_name=$(basename ${}) while read line; do
echo "${line}"
done < ${script_dir}/file.info

Shell按行读取文件的3种方法的更多相关文章

  1. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

  2. shell按行读取文件

    这工作小半年了发现以前学的那么多流弊技能都不怎么用,倒是shell用的很多,自己已经从shell小菜鸟一步步走过来,已经要变成大菜鸟=.= 经常需要用shell按行读取配置文件,自己在上面踩了很多坑, ...

  3. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  4. Shell逐行读取文件的3种方法

    方法1:while循环中执行效率最高,最常用的方法. while read linedoecho $linedone  < filename 注释:这种方式在结束的时候需要执行文件,就好像是执行 ...

  5. php 读取文件的几种方法

    文件操作的三个步骤,打开,操作,关闭.$fopen=fopen(路径,方式),fwrite($fopen,写入的字符串);fclose($fopen). 其中打开方式有如下几种方式: 模式 描述 r ...

  6. shell 按行读取文件的内容

    test.py: #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr ...

  7. shell 按行读取文件

    #!/bin/bash count= //赋值语句,不加空格 cat test | while read line //cat 命令的输出作为read命令的输入,read读到的值放在line中 do ...

  8. python 逐行读取文件的三种方法

    方法一: 复制代码代码如下: f = open("foo.txt")             # 返回一个文件对象  line = f.readline()             ...

  9. shell总结:读取文件、参数、if、分割字符串、数组长度、空文件、变量赋值、多进程、按行切割文件、查看线程

    Reference: http://saiyaren.iteye.com/blog/1943207 1.     Shell  读取文件和写文件 for line in $(<top30000. ...

随机推荐

  1. [转]MySQL查询缓存清空

    本文转自:https://www.cnblogs.com/wangyiwei/p/7765457.html 可以通过下面的SQL查看当前查询缓存相关参数状态:   SHOW VARIABLES LIK ...

  2. OAuth2.0 授权码理解

    OAuth2.0授权模式   本篇文章介绍OAuth的经典授权模式,授权码模式   所谓授权无非就是授权与被授权,被授权方通过请求得到授权方的同意,并赋予某用权力,这个过程就是授权.   那作为授权码 ...

  3. JQuery官方学习资料(译):Attributes

    Attributes     元素的属性可以为你的应用程序包含有用的信息,重要的是能够获取和设置它. .attr()方法     .attr()方法是可获取和可设置的,在设置状态下,.attr()可以 ...

  4. Prism 学习:从本地目录加载 Module

    在 Prism 中,将外部模块加载到主程序有以下几种方式:Code.XAML.配置文件.指定模块目录:其中,如果要使用 Code 方式来加载 Module,则需要将该 Module 引用到当前项目中: ...

  5. FastDFS搭建

    a.上传安装时需要的文件 ①:FastDFS_v5.05.tar.gz ②:fastdfs-nginx-module_v1.16.tar.gz ③:libfastcommon-1.0.7.tar.gz ...

  6. 微服务创建——Ubuntu搭建GitLab

    Ubuntu呢,用的国产麒麟,可能对于用习惯了Windows操作系统的人来说使用UKylin会很难受吧,开发的人倒没什么,不过就是命令行的问题 那么,怎么搭建一个完整的GitLab呢,一步步来操作吧, ...

  7. 为链表数据结构实现iterator接口

    iterator作用 为所有的数据结构提供统一的访问方式. 接口对象 接口对象一共有3个方法,next()方法.return()方法.throw()方法. next() 必填 用于for..of迭代. ...

  8. Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】

    热烈庆祝 Laravel 5.5 LTS 发布! 实际上 Laravel 上一个 LTS 选择 5.1 是非常不明智的,因为 5.2 增加了许许多多优秀的特性.现在好了,大家都用最新的长期支持版本 5 ...

  9. angular select 默认值

    <select ng-model="selected" ng-options="x.id as x.name for x in users">< ...

  10. angular 拼接html 事件无效

    主要是要引用$compile方法