当前位置:网站首页>10、标准I/O输入输出重定向及管道
10、标准I/O输入输出重定向及管道
2022-06-28 21:39:00 【51CTO】
标准输入及输出
linux提供三种I/O设备
标准输入stdin -0 默认接受来自终端的输入
标准输出stdout -1 默认输出到终端窗口
标准错误stderr -2 默认输出到终端窗口
I/O重定向redirect
I/O重定向:将默认的输入,输出或错误对应的设备改变,指向新的目标
标准输出和错误重定向
stdout和stderr可以被重定向到指定文件,而非默认的当前终端; 格式 :命令 操作符 文件名
1> 或 > 把stdout重定向到文件
2> 把stderr重定向到文件
&> 把所有输出重定向到文件
以上如果文件已存在,文件内容会被覆盖
set -c 禁止将内容覆盖已有文件,但可最佳,利用 >| 仍可强制覆盖
set +c 允许覆盖,默认设置
使用 >>可以在原有的内容上追加内容(>> 2>> &>>)

标准输出和错误输出到不同的位置


合并标准输出和错误输错为同一个数据流进行重定向(可以使用&>> '1>file 2>&1')



合并多个程序输出 (cmd1;cmd2)或者{ cmd1;cmd2;...;}合并多个程序的stdout {}命令末尾需要有分号且cmd1前面需要有一个空格





清除大文件命令(上一文已讲解)
cat /dev/null >/data/file.log
分别重定向

将输出文件进行丢弃 (\dev\null 空设备文件)

标准输入重定向
从文件中导入stdin,代替当前终端的输入设备,使用<来重定向标准输入,某些命令能够接受从文件中导入的stdin

tr命令 (转换字符和删除字符)
tr [option] .. set1 [set2]
选项
-d --delete:删除所有属于第一字符集的字符

-s --squeeze-repeats:把连续重复的字符已单独一个字符表示,即去重(注:需连续,不连续不会)

-t --truncate-setl:将第一个字符集对应字符转化为第二字符集对应的字符

-c -C --complent:取字符集的补集




标准输入重定向
可以利用<进行重定向
单行重定向 ctrl+d可以终止输入




多行重定向
使用 "<<终止词”命令从键盘把多行导向给stdin,知道终止词位置之前的所有文本都发送给stdin;终止词可以是一个或多个符合如 !,@,$,EOF 等其中EOF比较常用



管道
管道(使用 | 表示,)可以用来连接多个命令
格式 命令1 | 命令2 | 命令3
功能说明:
将命令1的stdout发送给命令2的stdin,命令2的stdout发送到命令3的stdin
所有命令会在当前shell进程的子shell进程中执行
组合多种工具的功能
注意;stderr默认不通通过管道转发,可以利用2&>1或 | &实现,格式如下: 命令1 2>&1 | 命令2 命令1 |& 命令2

管道中的-符号
tar -cvf - /home | tar -xvf -
将home里面的文件打包,但打包的数据不是记录到文件,而是传送到stdout,经过管道后,将tar -cvf - /home 传送给后面的 tar -xvf -,后面的这个-则是去前一个命令的stdout,因此就不需要临时file

tee命令
可以利用tee命令重定向到多个目标
命令1 | tee [-a] 文件名 | 命令2
-a 追加 功能:保存不同阶段的输出 复杂管道的故障排除 同时查看和记录输出

边栏推荐
- Hashicorp/raft introduction and source code analysis (III): introduction to cluster node recovery
- To be a cross-border e-commerce, you must learn to use PRA software, free your hands and improve efficiency!
- LeetCode213. House raiding II
- 什么是接口?什么是接口测试?
- Ehcache configuration data, convenient for self checking
- 接口用例设计
- Interface use case design
- 华为云的AI深潜之旅
- CVPR 2022 𞓜 a creative and aesthetic text generation method! Support any input
- Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
猜你喜欢

E-commerce is popular, how to improve the store conversion rate?

河狸生存记:90后女博士与AI开发者们

Usage example of qjsonobject

Interface test process

直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
![Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function](/img/16/2edfc478a56e5b5e7299621ac778c2.jpg)
Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function

城市大脑知识图谱构建及应用研究
![Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue](/img/e4/60b063a3131d92e40b5fa610bbec17.jpg)
Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue
![[software test] 2022 national unified college enrollment examination](/img/9a/d76d7eb30a097d364fef28c2230e1a.png)
[software test] 2022 national unified college enrollment examination
随机推荐
Web 自动化环境搭建
HTTP Caching Protocol practice
AI deep dive of Huawei cloud
Flask - Summary
Webrtc audio and video development - experience
How to analyze the relationship between enterprise digital transformation and data asset management?
[webapi] return dynamic list dynamic
视觉弱监督学习研究进展
16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
Recommend two high-quality Wallpaper software
Leetcode: merge K ascending linked lists_ twenty-three
精通数据分析能力,收入翻倍?什么才是最强竞争力
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
How to make up the PMP Exam? How much is the make-up exam?
[linq]c list type grouping sum
Why use the rust language?
Pyechart drawing multiple Y-axis line graphs
LeetCode121. The best time to buy and sell stocks
Leetcode56. consolidation interval
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.