Shell中while循环的done 后接一个重定向<
读文件的方法:
第一步: 将文件的内容通过管道(|)或重定向(<)的方式传给while
第二步: while中调用read将文件内容一行一行的读出来,并付值给read后跟随的变量。变量中就保存了当前行中的内容。
例如读取文件/sites/linuxpig.com.txt
1)管道的方式:
cat /sites/linuxpig.com.txt |while read LINE
do
echo $LINE
done
当然也可以将cat /sites/linuxpig.com.txt 写成一些复杂一些的,比如:
示例1:
find -type f -name "*.txt" -exec cat |while read LINE
do
echo $LINE
done
可以将当前目录所有以 .txt 结尾的文件读出
示例2:
grep -r "linuxpig.com" ./ | awk -F":" '{print $1}' | cat |while read LINE
do
echo $LINE
done
可以将含有 "linuxpig.com" 字符串的所有文件打开并读取。。
示例没有实际测试,如果使用请先测试。。。。。:-)
2)重定向的方式:
2.1 利用重定向符<
while read LINE
do
echo $LINE
done < /sites/linuxpig.com.txt
2.2 利用文件描述符(0~9)和重定向符 <
exec 3<&0 #先将文件描述符0复制到文件描述符3,也就是给文件描述符0做个备份
exec 0</sites/linuxpig.com.txt #读文件到文件描述符0
while read LINE # 此变量是读来自stdin(即描述符0)的数据
do
echo $LINE
done
exec 0<&3 #将文件描述符3复制给文件描述符0(恢复0从键盘读入)
Shell中while循环的done 后接一个重定向<的更多相关文章
- shell中的循环
shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...
- (八)shell中的循环结构
1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...
- shell中for循环
shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...
- shell中for循环总结
关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...
- Shell中的循环语句实例
1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do echo number $x done 注:" ...
- shell中,我们可以通过简单的一个判断来判断命令是否存在
shell中,我们可以通过简单的一个判断来判断命令是否存在 which "Command" > /dev/null if [ $? -eq 0 ] then echo com ...
- shell中while循环的陷阱
在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...
- Linux shell编程 4 ---- shell中的循环
1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...
- shell中的循环语句
for语法格式 for var in list;do commands done 其中list可以包含: 1) 直接写 for alpha in a b c d;do echo $alpha done ...
随机推荐
- how are you
#include<stdio.h> int main(){ char sentence[100]; int len=0,j,wordlen=0; gets(sentence ...
- The Golden Age CodeForces - 813B (数学+枚举)
Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a ...
- Visual Studio 2017 社区版的安装与组件修改(C++)
0. 环境描述 需求:用VS2017做C++简易开发. 操作系统:Windows 8.1. 1. 下载 MSDN下载VS2017社区版. https://msdn.itellyou.cn/ 下载后: ...
- PAT 1076 Wifi密码
https://pintia.cn/problem-sets/994805260223102976/problems/994805262622244864 下面是微博上流传的一张照片:“各位亲爱的同学 ...
- git查看分支图
git log --graph --decorate --oneline --simplify-by-decoration --all
- React 组件库框架搭建
前言 公司业务积累了一定程度,需要搭建自己的组件库,有了组件库,整个团队开发效率会提高恨多. 做组件库需要提供开发调试环境,和组件文档的展示,调研了几个比较主流的方案,如下: docz 配置简单,功能 ...
- codeforces742B
Arpa’s obvious problem and Mehrdad’s terrible solution CodeForces - 742B There are some beautiful gi ...
- C# 事件 订阅与发布
两种方式: 一: //服务器 public class Server { //服务器发布的事件 public event Action<string> MyEvent; public vo ...
- vimrc 的配置
windows syntax on set nocompatible set guifont=Consolas:h17 set linespace=0 color molokai set clipbo ...
- C代码快速构建框架
#include "stdio.h" typedef char int8_t; typedef short int16_t; typedef int int32_t; typede ...