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. 翻译:replace into语句(已提交到MariaDB官方手册)

    本文为mariadb官方手册:REPLACE INTO的译文. 原文:https://mariadb.com/kb/en/replace/ 我提交到MariaDB官方手册的译文:https://mar ...

  2. YARN集群的mapreduce测试(一)

    hadoop集群搭建中配置了mapreduce的别名是yarn [hadoop@master01 hadoop]$ mv mapred-site.xml.template mapred-site.xm ...

  3. 安装并使用Jupyter Notebook

      Jupyter Notebook是一个交互式笔记本,支持运行 40 多种编程语言.笔者在写博客文章时,常常需要贴代码,一贴就是一大堆代码,这样不便于读者阅读,而使用Jupyter Notebook ...

  4. 【转】AJAX中JSON数据的返回处理问题

    AJAX处理复杂数据时,便会使用JSON格式.常用在对数据库的数据查询上.在数据库查询到数据后,便可在处理页面直接将数据转为JSON格式,然后返回. 本篇主要讨论:jQuery中,JSON数据在AJA ...

  5. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  6. 02-Java中的对象和类

    面向对象: 程序由对象构成,每个对象包含对用户公开的特定功能部分(public)和隐藏实现部分(private). 类: 构造对象的模板 对象: 对象的行为 --- 可以对对象施加的操作(方法) 对象 ...

  7. redirection in linux

    2>&1 # Redirects stderr to stdout. # Error messages get sent to same place as standard output ...

  8. MySQL添加新用户、为用户创建数据库、为新用户分配权限

    登录MySQL [root@VM_0_2_33_centos /]#mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 mysql>'; ...

  9. [JavaScript]手机滑动图片

    思路 1.用ul ,li 来装载滑动的图片,超出部分隐藏 2.滑动是通过改变ul的位置来实现 布局 模块 1: 根据li元素个数去设置ul的宽度 1.1 获取ul元素 1.2 获取li元素的个数 1. ...

  10. 关于TensorFlow你需要了解的9件事

    关于TensorFlow你需要了解的9件事 https://mp.weixin.qq.com/s/cEQAdLnueMEj0OQZtYvcuw 摘要:本文对近期在旧金山举办的谷歌 Cloud Next ...