Shell按行读取文件的3种方法
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种方法的更多相关文章
- shell脚本,按行读取文件的几种方法。
第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...
- shell按行读取文件
这工作小半年了发现以前学的那么多流弊技能都不怎么用,倒是shell用的很多,自己已经从shell小菜鸟一步步走过来,已经要变成大菜鸟=.= 经常需要用shell按行读取配置文件,自己在上面踩了很多坑, ...
- python_基础学习_01_按行读取文件的最优方法
python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...
- Shell逐行读取文件的3种方法
方法1:while循环中执行效率最高,最常用的方法. while read linedoecho $linedone < filename 注释:这种方式在结束的时候需要执行文件,就好像是执行 ...
- php 读取文件的几种方法
文件操作的三个步骤,打开,操作,关闭.$fopen=fopen(路径,方式),fwrite($fopen,写入的字符串);fclose($fopen). 其中打开方式有如下几种方式: 模式 描述 r ...
- shell 按行读取文件的内容
test.py: #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr ...
- shell 按行读取文件
#!/bin/bash count= //赋值语句,不加空格 cat test | while read line //cat 命令的输出作为read命令的输入,read读到的值放在line中 do ...
- python 逐行读取文件的三种方法
方法一: 复制代码代码如下: f = open("foo.txt") # 返回一个文件对象 line = f.readline() ...
- shell总结:读取文件、参数、if、分割字符串、数组长度、空文件、变量赋值、多进程、按行切割文件、查看线程
Reference: http://saiyaren.iteye.com/blog/1943207 1. Shell 读取文件和写文件 for line in $(<top30000. ...
随机推荐
- 第12章 Linux配置定时任务详解
12.1 配置定时任务 首先需弄清的概念: (1).crond是一个daemon类程序,路径为/usr/sbin/crond.默认会以后台方式启动,service或systemd方式启动crond默认 ...
- Go基础系列:Go中的方法
Go方法简介 Go中的struct结构类似于面向对象中的类.面向对象中,除了成员变量还有方法. Go中也有方法,它是一种特殊的函数,定义于struct之上(与struct关联.绑定),被称为struc ...
- mac os下vscode快捷键
全局 Command + Shift + P / F1 显示命令面板 Command + P 快速打开 Command + Shift + N 打开新窗口 Command + W 关闭窗口 基本 Co ...
- #WEB安全基础 : HTML/CSS | 0x2初识a标签
教你点厉害玩意,尝尝HTML的厉害! 我为了这节课写了一些东西,你来看看
- 51nod1238 最小公倍数之和 V3(莫比乌斯反演)
题意 题目链接 Sol 不想打公式了,最后就是求一个 \(\sum_{i=1}^n ig(\frac{N}{i})\) \(g(i) = \sum_{i=1}^n \phi(i) i^2\) 拉个\( ...
- PyCharm快捷键使用
- iOS---------Xcode中添加预编译pch文件
第一步:打开项目,com+N,将页面滑动最下面如图 第二步:创建pch文件 第三步:修改buildsetting配置文件 在搜索框里输入prefix搜索一下,比较好找 1.将Pr ...
- Java用户自定义函数
用户除了可以使用JavaScript的内置函数之外,还可以自己定义函数.自定义函数有3种方法. 使用关键字 function 构造 语法: function funcName([param1][,pa ...
- Android为TV端助力 关于android的一些基础知识
怕自己以后忘了,所以在这里先写写! equal和==的区别是,一个用于判断字符串,一个用于判断int是否相等 equal比较的是对象,==比较的是值
- Linux Xshell连接Linux服务器时报错Socket error Event: 32 Error: 10053
问题描述 在用Xshell连接Linux服务器时,出现错误提示"Socket error Event: 32 Error: 10053. Connection closing...Socke ...