Linux-IO重定向与管道
1. 输入与输出
- 标准输入 STDIN
文件描述符:0,默认:键盘输入 - 标准输出 STDOUT
文件描述符:1,默认:屏幕输出 - 错误输出 STDERR
文件描述符:2,默认:屏幕输出
2. 标准输出重定向
覆盖输出 >
追加输出 >>
注意:shell的内嵌命令set可以设置是否允许输出重定向至已存在的文件
set -C:禁止输出重定向至已存在的文件
set +C:允许输出重定向至已存在的文件
示例:标准输出重定向到文件(实际动作:先创建文件,再向其中写入标准输出内容)
[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, sh
[root@VM_41_201_centos ~]# ls -m > ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
[root@VM_41_201_centos ~]# ls -m >> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
anaconda-ks.cfg, ls.txt, sh
[root@VM_41_201_centos ~]#
3. 错误输出重定向
覆盖输出 2>
追加输出 2>>
示例:错误输出重定向到文件
[root@VM_41_201_centos ~]# lss 2> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# lss 2>> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]#
4. 合并标准输出与错误输出重定向
- 覆盖输出 &>
COMMAND &> 文件
COMMAND > 文件 2>&1 #不推荐这种形式,难记,不好理解
COMMAND > 文件A 2> 文件B
- 追加输出 &>>
COMMAND &>> 文件
COMMAND >> 文件 2>&1 #不推荐这种形式,难记,不好理解
COMMAND > 文件A 2> 文件B
示例:合并标准输出与错误输出重定向
# 标准输出
[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, ls.error, ls.txt, sh
# 标准输出重定向到文件
[root@VM_41_201_centos ~]# ls -m &> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
# 错误输出重定向到文件(追加)
[root@VM_41_201_centos ~]# lss &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
-bash: lss: 未找到命令
# 合并标准输出与错误输出(标准输出,覆盖)
[root@VM_41_201_centos ~]# ls -m > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
# 合并标准输出与错误输出(错误输出,覆盖)
[root@VM_41_201_centos ~]# lss > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
# 合并标准输出与错误输出(错误输出,追加)
[root@VM_41_201_centos ~]# lsss >> ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令
# 合并标准输出与错误输出(错误输出,追加 ,另一种方式)
[root@VM_41_201_centos ~]# lsls &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令
-bash: lsls: 未找到命令
[root@VM_41_201_centos ~]#
#使用COMMAND > 文件A 2> 文件B、COMMAND >> 文件A 2>> 文件B的形式
[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg ls.error ls.txt sh tee.txt
[root@VM_41_201_centos ~]# ls -m > A 2> B
[root@VM_41_201_centos ~]# ls -m
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat A
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat B
[root@VM_41_201_centos ~]# sl -m > A 2> B
[root@VM_41_201_centos ~]# cat A
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
[root@VM_41_201_centos ~]# lss -m >> A 2>> B
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]#
5. 输入重定向
输入重定向相比较而言,就比较简单了,而且用得相对较少
一般用法:将文件作为输入重定向到命令
命令 < 文件
示例:将文件输入重定向至命令wc统计文件行数:
[root@VM_41_201_centos ~]# wc -l < anaconda-ks.cfg
148
[root@VM_41_201_centos ~]#
6. 管道
管道用于连接多个命令(程序),将前一个命令的结果重定向,作为后一个命令的输入
COMMAND1 | COMMAND2 | COMMAND3 | ...
示例:将文件内容通过管道重定向到命令(其效果类似输入重定向)
[root@VM_41_201_centos ~]# cat anaconda-ks.cfg | wc -l
148
[root@VM_41_201_centos ~]#
7. 相关命令
以下几个命令经常与IO重定向(> >> 2> 2>> &> &>> )及管道(|)结合使用
- tee
read from standard input and write to standard output and files
tee命令比较特殊:从标准输入读取、写入标准输出、写入文件(同时干了3件事,一箭三雕),IO重定向与tee更配哦。
示例:tee命令(执行结果既输出到了指定文件,又输出到了terminal)
[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg ls.error ls.txt sh
[root@VM_41_201_centos ~]# ls -m | tee tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]# cat tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]#
- tr - translate or delete characters
tr命令用于替换及删除字符
SYNOPSIS
tr [OPTION]... SET1 [SET2]
OPTIONS
-c, -C, --complement
use the complement of SET1
-d, --delete
delete characters in SET1, do not translate
-s, --squeeze-repeats
replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
-t, --truncate-set1
first truncate SET1 to length of SET2
示例
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# cat ls.error | tr b B
-Bash: lss: 未找到命令
-Bash: lss: 未找到命令
[root@VM_41_201_centos ~]#
- 输入重定向分界符:<<
允许用户一直输入,直到输入的内容匹配<<指定的字符串
# EOF可以用其它字符代替,习惯用EOF,end of file
命令 << EOF
示例:
[root@VM_41_201_centos ~]#
[root@VM_41_201_centos ~]# cat << EOF
> 1.a
> 2.b
> 3.c
> EOF
1.a
2.b
3.c
[root@VM_41_201_centos ~]#
Linux-IO重定向与管道的更多相关文章
- Linux IO重定向和管道
计算机组成部分: 由io . 控制器.计算器.存储器组成 IO: input output 计算机里面通过终端窗口实现输入和输出,键盘鼠标屏幕这些只是手段,真正完成输入输出的是终端窗口 标准输入.出. ...
- 四、IO重定向和管道以及基本文本处理工具
一.三种IO设备 程序:数据+指令 或 数据结构+算法程序必须能够读入输入然后经过加工来产生结果,其接受的输入可以是变量.数组.列表.文件等等,生产出来的结果可以使变量.数组.列表.文件等等.即:程序 ...
- IO重定向与管道
一.三种IO设备 程序:数据+指令 或 数据结构+算法 程序必须能够读入输入然后经过加工来产生结果,其接受的输入可以是变量.数组.列表.文件等等,生产出来的结果可以使变量.数组.列表.文件等等.即: ...
- Linux Shell 重定向与管道【转帖】
by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...
- Linux中重定向及管道
1重定向1.1 重定向符号 > 输出重定向到一个文件或设备 覆盖原来的文件 >! 输出重定向到一个文件或设备 强制覆盖原来的 ...
- Linux数据流重定向与管道
数据流重定向简单来说就是把原本应该输出到某处(比如说屏幕)的数据,重定向其输出目的地,到其他的地方(比如文件). linux中的输入与输出: 标准输入(stdin):默认从键盘输入 标准输出(stdo ...
- Linux的重定向与管道
(1).输出重定向 定义:将命令的标准输出结果保存到指定的文件中,而不是直接显示在显示器上. 输出重定向使用>和>>操作符. 语法:cmd > filename,表示将标准输出 ...
- 【Linux】重定向与管道
重定向 redirection 每个命令有输入源和输出目的地,默认行为,是标准输入和标准输出.大多数情况,标准输入是键盘,标准输出是屏幕.可以为单独的操作修改输入和输出,这就是重定向.重定向可以使某个 ...
- linux - 输入输出重定向 及 管道
> 正确结果重定向 2> 错误结果重定向 &> 正确和错误全部重定向 >> 追加,其它同> 标准输出实际上就是显示器,比如我们使用cat命令打开一个文件,文 ...
- linux学习14 Linux运维高级系统应用-glob通配及IO重定向
一.回顾 1.bash基础特性:命令补全,路径补全,命令引用 2.文件或目录的复制,移动及删除操作 3.变量:变量类型 存储格式,数据表示范围,参与运算 二.bash的基础特性 1.globbing: ...
随机推荐
- Linux快速安装apache+mysql+php环境
yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-postgresql ph ...
- Linux网络端口命名规则,一致性网络设备命名
参考文档: https://www.cnblogs.com/pipci/p/9229571.html 一致性网络设备命名,即Consistent Network Device Naming. 一.服务 ...
- Hadoop知识点
1.小文件合并:如果文件有一定的规律或者是在同一个文件夹下,可以采用获取文件夹下所有的文件,通过流进行合并,然后再存到hdfs上. 2.mapreduce的优点:1.离线计算.2.高容错性,一个节点挂 ...
- 常用jvm参数
如果你是Eclipse ,可以通过 run -> Run Configurations->Arguments 添加-XX:+PrintGCDetails 打开gc日志 -Xmx 设置jav ...
- IOS初级:UIAlertController
- (IBAction)signOutAction:(id)sender { //初始化,StyleActionSheet是对话框的样式 UIAlertController *alert = [UIA ...
- tomcat运行监控脚本,自动启动
参见:http://www.cnblogs.com/coffee_cn/p/8279165.html monitor.sh #!/bin/sh monitorlog=/usr/local/tomcat ...
- EasyUI DataGrid 获得分页信息
var b = $('#SBDiv_1_DateGrid').datagrid('options'); console.info(b); 具体需要哪些字段,可以通过火狐debug,然后自己找需要的信息 ...
- 使用yarn 安装 Vue-DevTools
1. 从 github 下载 vuejs/vue-devtools https://github.com/vuejs/vue-devtools/archive/dev.zip 2.安装yarn 及 编 ...
- SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写
spring配置文件中需要干的事情 (一)开启 Service与pojo包的注解扫描 注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为 ...
- 扩展方法(深入理解c#)
1. 静态类到扩展方法: 许多方法可能都适合转为扩展方法,只要具有以下特征: 1)你想为一个类型添加一些成员: 2)你不需要为类型的实例添加更多的数据: 3)你不能改变类型本身,因为是别人的代码 2. ...