Linux shell 重定向输入和输出

Linux shell 重定向输入和输出

  • 1. Standard I/O streams
  • 2. Redirecting to and from the standard file handles (标准文件句柄的重定向)
    • 2.1. `command > file`
    • 2.2. `command >> file`
    • 2.3. `command 2> file`
    • 2.4. `command 2>> file`
    • 2.5. `command < file`
    • 2.6. `command < infile > outfile`
    • 2.7. `command > file 2>&1`
    • 2.8. `command 2>&1 > file`
  • References

In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
在计算机领域,重定向是大多数命令行解释器所具有的功能,包括各种可以将标准流重定向用户规定目的地的 Unix shells。

1. Standard I/O streams

In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection.
源自 Bourne shell 的许多 Unix shell,可以将一个数字 (文件描述符) 放在重定向符号前,这样可以影响用于重定向的数据流。

The Unix standard I/O streams are:

File DescriptorNameDescription操作符
0stdinstandard input (标准输入)<, <<
1stdoutstandard output (标准输出)>, >>, 1>, 1>>
2stderrstandard error (标准错误输出)2>, 2>>

文件描述符 (File Descriptor) 是进程对其所打开文件的索引,形式上是个非负整数。Linux 会为每个运行的进程都分配这三个文件。

stdin 默认输入源是 Keyboard,stdoutstderr 默认输出目的地是 Text Terminal。

默认情况下,command > filestdout 重定向到 filecommand < filestdin 重定向到 file

在这里插入图片描述

使用 >>> 时,默认为 stdout (标准输出) 重定向,> 就是 1>1> 之间不能有空格。File Descriptor 0, 1, 2 与它后面的操作符 >, >>, <, << 是一个整体。

ls -a -l > yongqiang.txt 等同于 ls -a -l 1> yongqiang.txt

(base) yongqiang@yongqiang:~/software$ ls -a -l
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun  4 22:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ ls -a -l > yongqiang.txt
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:14 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
-rw-r--r--  1 yongqiang yongqiang         0 Jun 16 15:14 yongqiang.txt
(base) yongqiang@yongqiang:~/software$

yongqiang.txt 文件中有 -rw-r--r-- 1 yongqiang yongqiang 0 Jun 16 15:14 yongqiang.txt 行。yongqiang.txt 文件是在 stdout (标准输出) 重定向之前创建的,需要准备好输出目的地之后,stdout 才会发送到该目的地。

(base) yongqiang@yongqiang:~/software$ ls -a -l
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ ls -a -l 1> yongqiang.txt
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
-rw-r--r--  1 yongqiang yongqiang         0 Jun 16 15:26 yongqiang.txt
(base) yongqiang@yongqiang:~/software$

< - 输入重定向,命令默认从键盘获得输入,< 重定向从其它设备获得输入 (文件等)。
>, 1> - 输出 stdout 重定向。命令执行输出 stdout 默认打印到屏幕上,> or 1> 重定向 stdout 到其它输出设备 (文件等)。

<< - 输入追加重定向
>> - 输出追加重定向

2. Redirecting to and from the standard file handles (标准文件句柄的重定向)

2.1. command > file

command 命令的 stdout 重定向到文件 file 中。如果 file 已经存在,则清空原有文件。

command > file executes command, placing the output in file, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will clobber any existing data in file.
command > file 命令执行 command,然后将 stdout 的内容存入 file。注意任何 file 内的已经存在的内容将被清空,然后写入新内容。

clobber [ˈklɒbə(r)]:vt. 使受到 (严重经济损失),狠揍,猛打,惩罚,彻底战胜 (或击败),狠击,极大地打击 n. 衣服,随身物品

echo "yongqiangcheng" > file.txt 命令清空文件的内容,然后将 "yongqiangcheng" 写入 file.txt

(base) yongqiang@yongqiang:~/software$ echo `date` > yongqiang.txt
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
Sun Jun 16 20:33:49 CST 2024
(base) yongqiang@yongqiang:~/software$

Note: 使用的是反引号 `, 而不是单引号 '。

2.2. command >> file

command 命令的 stdout 重定向到文件 file 中。如果 file 已经存在,则把信息加在原有文件后面。

To append output to the end of the file, rather than clobbering it, the >> operator is used: command1 >> file.
command1 >> filestdout 追加到文件末尾,使用 >> 操作符。

echo "yongqiangcheng" > file.txt 命令将 "yongqiangcheng" 追加到 file.txt 的后面。

2.3. command 2> file

For example, command 2> file executes command, directing the standard error stream to file.
执行 command,然后将标准错误 stderr 输出重定向到文件 file,清空文件中已有内容,然后写入新内容。

2.4. command 2>> file

执行 command,然后将标准错误 stderr 输出重定向追加到文件 file 末尾。

2.5. command < file

本来需要从键盘获取输入的命令会转移到文件读取内容。

Using command < file executes command, with file as the source of input, as opposed to the keyboard, which is the usual source for standard input.
执行 command,使用 file 作为用来替代键盘的输入源。

2.6. command < infile > outfile

command < infile > outfile combines the two capabilities: command reads from infile and writes to outfile.
同时替换输入和输出,执行 command,从文件 infile 读取内容,然后将 stdout 写入到 outfile 中。

2.7. command > file 2>&1

To write both stderr and stdout to file, the order should be reversed. stdout would first be redirected to the file, then stderr would additionally be redirected to the stdout handle that has already been changed to point at the file: command > file 2>&1.
首先将 stdout 重定向到 file,然后 stderr 将重定向到已经更改为指向 filestdout 句柄。

In shells derived from csh (the C shell), the syntax instead appends the & (ampersand) character to the redirect characters, thus achieving a similar result. The reason for this is to distinguish between a file named 1 and stdout, i.e. cat file 2>1 vs cat file 2>&1. In the first case, stderr is redirected to a file named 1 and in the second, stderr is redirected to stdout.
2>&1stderr 重定向到 stdout21 分别是 stderrstdout 的文件描述符。为了区别普通文件,当将 stderrstdout 作为重定向目的地时,需要在文件描述符前加上 &为了将 stdout 与文件名为 1 的文件区分开来。例如对于 cat file 2>1cat file 2>&1,前者会将 stderr 重定向至叫做 1 的文件,后者则将其重定向至 stdout

A simplified but non-POSIX conforming form of the command, command > file 2>&1 is: command &>file or command >&file.

2.8. command 2>&1 > file

It is possible to use 2>&1 before > but the result is commonly misunderstood. The rule is that any redirection sets the handle to the output stream independently. So 2>&1 sets handle 2 to whatever handle 1 points to, which at that point usually is stdout. Then > redirects handle 1 to something else, e.g. a file, but it does not change handle 2, which still points to stdout.
可以将 2>&1 放置在 > 前,但是这样并不能达到我们想要的效果。

In the following example, stdout is written to file, but stderr is redirected from stderr to stdout, i.e. sent to the screen: command 2>&1 > file.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] Redirection (computing), https://en.wikipedia.org/wiki/Redirection_(computing)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/715888.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

餐饮食堂安全守护者:可燃气体报警器故障处理与检测要点解析

在餐饮食堂中&#xff0c;可燃气体报警器的正常运行对于预防火灾和保障人员安全至关重要。 接下来&#xff0c;佰德将围绕可燃气体报警器的故障现象识别、原因排查、安全操作准则、专业工具与备件、故障处理步骤、验证与测试以及维护与保养建议等方面进行详细阐述&#xff0c;…

VS2022打开.netcore2.2 问题解决

1.vs2022运行时一直提示异常 2.解决方法&#xff0c;双击当前的项目修改xxxx.csproj文件 把当前的版本修改为2.2.0即可重新编译运行

低代码开发平台

a.本质&#xff1a;降本增效的体系 1.强制统一组件库复用 2.提升系统一致性 3.降低开发资源投入 一、强制统一组件库复用&#xff1a;物料堆的建立 物料堆的形态&#xff1a;直联、整合 1.直联必须一层嵌套一层&#xff1a;el-form el-form-item el-input 2.整合是经过优…

快慢指针技巧

快慢指针技巧 在说快慢指针之前&#xff0c;我们先说一下双指针。 双指针 双指针&#xff1a;使用两个指针来解决问题。 所谓的指针其实就是指数组的下标&#xff0c;或者链表的节点的地址。 我们以数组为例介绍一下。 有两个指针分别存储着数组的两个下标&#xff0c;这就…

秋招突击——6/15——复习{(树形DP)树的最长路径,(单调队列优化DP)——最大子序和,无重复最长子串}——新作{四数之和}

文章目录 引言复习树形DP——树的最长路径实现代码答疑 单调队列优化DP——最大子序和个人实现思路参考思路分析实现代码 无重复最长字串思路分析实现代码 新作四数之和实现思路需要注意的问题 参考代码分析思路实现代码 总结 引言 今天好好看看树的最长的路径&#xff0c;自己…

JavaWeb之初识Tomcat

Tomcat 轻量级应用服务器、JSP、Servlet Tomcat目录结构 在IDEA中创建web项目 在这里不使用maven构建项目&#xff0c;这种方式后面会更新 新建一个java项目File -> Project Settings -> Facets -> -> Web -> OK ( 此时src目录下有一个web目录 )Edit ->…

39、基于深度学习的(拼音)字符识别(matlab)

1、原理及流程 深度学习中常用的字符识别方法包括卷积神经网络&#xff08;CNN&#xff09;和循环神经网络&#xff08;RNN&#xff09;。 数据准备&#xff1a;首先需要准备包含字符的数据集&#xff0c;通常是手写字符、印刷字符或者印刷字体数据集。 数据预处理&#xff1…

ElasticSearch + kibana:类型声明

当我们使用 kibana 创建索引时&#xff0c;如果不申明数据类型&#xff0c;默认字符串赋予 text类型&#xff0c;如下图所示 接下来我们继续创建多条数据如下&#xff1a; 下面我们来检索下&#xff1a; 通过以上两个案例我们发现&#xff0c;使用 match 模糊查询 li-3 明明…

智利企鹅濒临灭绝,回顾曾仕强的2025年预言!实干才是硬道理——早读(逆天打工人爬取热门微信文章解读)

你相信我们5000年凝结的精华易经吗&#xff1f; 引言Python 代码第一篇 洞见 有人晒出高考后家长支出清单&#xff0c;我觉得是时候告诉孩子挣钱的真相了第二篇 视频新闻结尾 引言 昨天有点破了 看小视频不小心看过头了 大概看了有2个小时 才醒悟过来 再接再厉呀&#xff01; …

vue3中如何使用pinia -- pinia使用教程(一)

vue3中如何使用pinia -- pinia使用教程&#xff08;一&#xff09; 安装使用创建 store使用 store访问修改 store 使用组合式 api 创建 store -- setup storepinia 和 hook 的完美结合如何解决上面的问题 使用 hook 管理全局状态和 pinia 有何优缺点&#xff1f;参考小结 pinia…

哈喽GPT-4o——对GPT-4o 文本创作的思考与看法

目录 用法1&#xff1a;创作小说用法2&#xff1a;创作散文用法3&#xff1a;创作诗歌1、古诗2、现代诗 用法4&#xff1a;创作儿童故事用法5&#xff1a;创作剧本 大家好&#xff0c;我是哪吒。 都说ChatGPT4o是目前文本创作的最强大模型&#xff0c;它都可以用于哪些方面的文…

ArcGIS 10.2软件安装包下载及安装教程!

今日资源&#xff1a;ArcGIS 适用系统&#xff1a;WINDOWS 软件介绍&#xff1a; ArcGIS是一款专业的电子地图信息编辑和开发软件&#xff0c;提供一种快速并且使用简单的方式浏览地理信息&#xff0c;无论是2D还是3D的信息。软件内置多种编辑工具&#xff0c;可以轻松的完成…

VirtualHere 允许通过网络远程使用 USB 设备,就像本地连接一样!

传统上&#xff0c;USB 设备需要直接插入计算机才能使用。有了 VirtualHere&#xff0c;就不再需要这样做&#xff0c;网络本身就变成了传输 USB 信号的电缆&#xff08;也称为 USB over IP、USB/IP、USB over WiFi、USB over Ethernet、USB 设备服务器&#xff09;。 此 USB …

Google谈出海:品牌「性价比」转向「心价比」

Google Marketing Live中国站活动现场 越来越多的中国全球化品牌基于对全球消费和海外地区的深刻洞察&#xff0c;不断提升产品研发和迭代能力&#xff0c;在海外消费者心中塑造「中国质造」和「中国智造」的新形象。2023年6月15日&#xff0c;凯度与Google合作发布《2023 凯…

JavaFX GridPane布局

网格布局 GridPane通常用于布局&#xff1a;表单布局 GridPane可以在行&#xff0c;列或单元格级别指定约束。 例如&#xff0c;我们可以设置包含输入文本字段的第二列&#xff0c;以在窗口调整大小时调整大小。 使用Java FX创建表格的时候&#xff0c;这个布局非常方便。 包…

开源低代码平台,JeecgBoot v3.7.0 里程碑版本发布

项目介绍 JeecgBoot是一款企业级的低代码平台&#xff01;前后端分离架构 SpringBoot2.x&#xff0c;SpringCloud&#xff0c;Ant Design&Vue3&#xff0c;Mybatis-plus&#xff0c;Shiro&#xff0c;JWT 支持微服务。强大的代码生成器让前后端代码一键生成! JeecgBoot引领…

7大功能特色 让这款信创传输软件受众行业青睐

信创传输软件&#xff0c;顾名思义&#xff0c;也就是能够支持信创环境的文件传输系统&#xff0c;并且需要具备强大的功能&#xff0c;可以满足各种复杂的传输需求。 这种软件可能具有以下特点和功能&#xff1a; 1、兼容性&#xff1a;能够与信创环境中使用的硬件设备、网络…

智慧校园可视化大屏,对教学教务的提升是肉眼可见的

随着信息技术的快速发展&#xff0c;智慧校园已经成为许多学校追求的目标。智慧校园可视化项目是一种通过信息化手段对教学教务进行管理和提升的创新方式。 智慧利用先进的技术手段&#xff0c;将校园各个环节的数据信息进行收集、分析和展示&#xff0c;从而实现对教学教务工…

用Python分析《三国演义》中的人物关系网

用Python分析《三国演义》中的人物关系网 三国演义获取文本文本预处理分词与词频统计引入停用词后进行词频统计构建人物关系网完整代码 三国演义 《三国演义》是中国古代四大名著之一&#xff0c;它以东汉末年到晋朝统一之间的历史为背景&#xff0c;讲述了魏、蜀、吴三国之间…

项目方案:社会视频资源整合接入汇聚系统解决方案(六)

目录 一、概述 1.1 应用背景 1.2 总体目标 1.3 设计原则 1.4 设计依据 1.5 术语解释 二、需求分析 2.1 政策分析 2.2 业务分析 2.3 系统需求 三、系统总体设计 3.1设计思路 3.2总体架构 3.3联网技术要求 四、视频整合及汇聚接入 4.1设计概述 4.2社会视频资源分类…