当前位置:网站首页>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 追加 功能:保存不同阶段的输出 复杂管道的故障排除 同时查看和记录输出

边栏推荐
- BOE was brilliant for the Winter Olympics, but revealed another Chinese technology enterprise dominating the world
- 视觉弱监督学习研究进展
- LeetCode:合并K个升序链表_23
- LeetCode:合并两个有序链表_21
- Leetcode: expand a binary tree into a linked list_ one hundred and fourteen
- 精通数据分析能力,收入翻倍?什么才是最强竞争力
- LeetCode1114. Print in sequence
- Openfire 3.8.2 cluster configuration
- Explanation and usage of sqrt() function
- 开通股票炒股账号安全吗?是靠谱的吗?
猜你喜欢
![[webapi] return dynamic list dynamic](/img/83/b0b36ddab6d74ccd89811cbb58d051.jpg)
[webapi] return dynamic list dynamic

Biovendor free light chain( κ and λ) Test steps of ELISA Kit

Laravel文档阅读笔记-Adding a Markdown editor to Laravel

rosdep update 使用小鱼fishros解决ros1/ros2问题 2022

How to analyze the relationship between enterprise digital transformation and data asset management?

ROS 2 Humble Hawksbill 之 f1tenth gym

Webrtc audio and video development - experience

The rogue downloader named by 315 is back

Pyechart drawing multiple Y-axis line graphs

PHP login problem
随机推荐
LeetCode1114. 按序打印
华为云的AI深潜之旅
Why use the rust language?
接口测试流程
Ehcache configuration data, convenient for self checking
Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue
How to analyze the relationship between enterprise digital transformation and data asset management?
Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
运动App如何实现端侧后台保活,让运动记录更完整?
Pat 1054 the dominiant color (20 points)
Web自动化工具选择
E-commerce is popular, how to improve the store conversion rate?
开通股票炒股账号安全吗?是靠谱的吗?
LeetCode213. 打家劫舍II
QT how the coordinates of one control are relatively fixed and displayed on another control (coordinate system)
认识Web自动化测试
Comprehensive evaluation of easy-to-use and powerful PDF reading software: PDF expert, marginnote, liquidtext, notability, goodnotes, Zotero
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
LeetCode1114. Print in sequence
Postman introduction and installation steps