Shell脚本之while read line的用法
while read line
do
…
done < file
[root@centos7 scripts]# cat /root/scripts/info
192.168.37.131 root 123456
192.168.37.132 root 123456 [root@centos7 scripts]# vim 6.sh
#!/bin/bash
file=/root/scripts/info
while read line;do
user=$(echo $line | cut -d " " -f 2)
ip=$(echo $line |cut -d " " -f 1)
passwd=$(echo $line|cut -d " " -f 3)
# cmd=$*
echo "-- $user -- $ip -- $passwd"
done < $file [root@centos7 scripts]# sh 6.sh
-- root -- 192.168.37.131 -- 123456
-- root -- 192.168.37.132 -- 123456
command | while read line
do
…
done
如果你还记得管道的用法,这个结构应该不难理解吧。command命令的输出作为read循环的输入,这种结构长用于处理超过一行的输出,当然awk也很擅长做这种事
案例:
[root@centos7 scripts]# cat /root/scripts/info
192.168.37.131 root 123456
192.168.37.132 root 123456 [root@centos7 scripts]# vim 5.sh
#!/bin/bash
file=/root/scripts/info
cat $file|while read line;do
user=$(echo $line | cut -d " " -f 2)
ip=$(echo $line |cut -d " " -f 1)
passwd=$(echo $line|cut -d " " -f 3)
# cmd=$*
echo "-- $user -- $ip -- $passwd"
done [root@centos7 scripts]# sh 5.sh
-- root -- 192.168.37.131 -- 123456
-- root -- 192.168.37.132 -- 123456
Shell脚本之while read line的用法的更多相关文章
- Shell脚本:while read line无法读取最后一行的问题
[1]Shell脚本:while read line无法读取最后一行的问题 刚刚利用shell脚本处理日志文件时,发现了一个问题:while read line无法读取到最后一行 通过编辑器可以看到待 ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
- Shell脚本、Shell脚本结构、date命令的用法、变量
1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构: #!/bin/bash 以#!/bin/bash开头,即以/bin/ba ...
- shell脚本编程之变量的小用法
变量赋值 ${parameter:-word}:如果parameter为空或未定义,则变量展开为"word":否则,展开为parameter的值: ${parameter:+wor ...
- shell脚本介绍 shell脚本结构和执行 date命令用法 shell脚本中的变量
- shell脚本,tee小工具的用法。
解释: tee是个工具 , 它的作用就是把标准输出,复制一份,扔文件里 ,原标准输出还输出,-a就相当于 >> 追加到文件里的意思. 不加就是 > 重定向到文件里去.
- 利用 tee 命令调试shell脚本中的管道
在编写shell脚本时,调试是个比较麻烦的事,特别是涉及到多层管道命令的时候,会产生多个中间结果,tee命令的作用是从标准输入中读取数据写入标准输出或文件中,利用它可以从管道中读取中间结果并写入本地临 ...
- Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法
一.需求背景 因此次项目的生产环境中部署了多套系统,每天会产生大量的日志(数百GB的量),侵占了服务器宝贵的存储资源空间.为了有效缓解服务器存储压力,考虑通过Linux的Shell脚本结合cr ...
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ...
- Shell脚本字符串匹配及日常命令工具 - 用法总结(技巧指南)
Shell提供了很多字符串和文件处理的命令,如awk.expr.grep.sed等命令,还有文件的排序.合并和分割等一系列的操作命令.下面重点总结下Shell字符串处理.文本处理以及各类命令及函数用法 ...
随机推荐
- MAC使用Graphviz包报错 failed to execute PosixPath('dot')
在使用LightGBM进行可视化时,用到了Graphviz包,在安装Graphviz包时遇到了以下问题. 错误描述: ExecutableNotFound: failed to execute Pos ...
- 前端-对js原型继承的简单举例
function A(name,color){ this.name=name; this.color=color; } A.prototype.getColor=function(){ retur ...
- [Unity3D 小Tricks] 如何修改Unity3d脚本默认模板?
众所周知,unity默认的模板总是Update()和Start(),但往往我们并不需要,每次都要手动删除非常麻烦. 但可以更改如下模板文件 C:\Program Files\Unity 2020.3. ...
- dead code?
public static void main(String[] args) { DriverBase dbase = new DriverBase(); dbase.driverBase(); dr ...
- snorkel SentenceNgramViewer 标记时出现 Integrity Error
Snorkel使用SQLAlchemy作为数据管理工具,在本地会生成一个.db文件,现有操作与.db文件中已有的记录重复,违反了数据库主键不能重复的性质 解决方式: You can load a la ...
- 当你的数据集是hdf5格式的文件时,肿么办?
最近,自己构建了一个卷积神经网络,从网上下载到的数据集是hdf5格式的,希望用这个数据集来训练一下自己构建的这个神经网络. 1. 什么是hdf5? HDF5是二进制数据格式,用于在磁盘上存储巨大的数值 ...
- SQL-while begin end
declare @i int set @i=1 while @i<=10000000begin insert into dbo.Persons (Age,Sex,Grade,Name) valu ...
- Vue的学习(1)
在学习Vue之前,首先大家要知道一个mvvm模式,何为mvvm模式呢,mvvm其实是有m,v和vm组成,类似与java里面的mvc模式,只不过mvc模式是针对于后台来说,而mvvm是针对于前台来说的, ...
- LSP原则中的逆变和协变
在复习过程中,LSP原则是个很重要的内容.这里先给出LSP原则的定义. LSP定义Functions that use pointers or referrnces to base classes m ...
- Vuex----Actions
Actions用于处理异步任务. 如果通过异步操作变更数据,必须通过 Action,而不能使用Mutation,但是在 Action中还是要通过触发Mutation的方式间接变更数据. 注意: 在Ac ...