Linux shell 重定向输入和输出

news2024/10/6 1:47:45

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)

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JavaFX GridPane布局

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

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

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

AI大模型-本科生24暑期实习NLP(Infra LLM)算法工程师面经

Brief Intro 今年暑假&#xff0c;在科研和工业界之间&#xff0c;我选择在国内工业界找一份实习&#xff0c;参与到百模大战的浪潮中&#xff0c;主要的意向是知名的LLM领域的独角兽&#xff0c;期望能避免做Dirty Work&#xff0c;在实习过程中也能被重视&#xff0c;做一些…

AI数据分析:集中度分析和离散度分析

在deepseek中输入提示词&#xff1a; 你是一个Python编程专家&#xff0c;要完成一个Python脚本编写的任务&#xff0c;具体步骤如下&#xff1a; 读取Excel表格&#xff1a;"F:\AI自媒体内容\AI行业数据分析\toolify月榜\toolify2023年-2024年月排行榜汇总数据.xlsx&qu…

浙江广厦大学第七届程序设计比赛(重现赛)(个人题解)(未完成)

前言&#xff1a; 今天晚上实验室的一场比赛&#xff0c;题目难度感觉还行&#xff0c;有几道题大家都没做出来&#xff0c;老规矩&#xff0c;这些没写出来的题都放在这&#xff0c;等我有能力补的时候再来写。 正文&#xff1a; 原比赛链接&#xff1a;(1条未读私信) 浙江广…

NASA数据:南极海洋生物资源

Antarctic Marine Living Resources (AMLR) program 南极海洋生物资源许可证 南极海洋生物资源保护委员会公约区受到管制。任何打算从该区域捕获海洋生物的人都必须获得许可证。 简介 美国是南极海洋生物资源保护委员会&#xff08;Commission for the Conservation of Anta…

热管式换热器

热管式换热器是一种高效、紧凑的换热设备&#xff0c;其核心部件是热管。热管技术基于热管内部工作介质&#xff08;通常是液体&#xff09;的相变原理来传递热量&#xff0c;能够实现快速、大温差的热量传输&#xff0c;特别适用于需要高效换热或者在空间受限条件下进行热能交…

【APP_汽修宝】数据采集案例APP_数据解密分析

如果不会写代码&#xff0c;那就出书、写博客、做视频、录播客。 &#x1f4da; S35赛季末王者昭君罗 关键代码定位 使用方法【逆向-快速定位关键代码】通过hook常用函数HashMap方法 动态分析 下面是我们通过访问目标页面时 Frida hook 捕获HashMap的调…

Linux之BCC 性能工具的移植和使用

一、bcc 工具 bcc 的全称&#xff1a;BPF Compiler Collection BCC&#xff08;BPF Compiler Collection&#xff09;是一个用于创建高效的内核跟踪和操作程序的工具包&#xff0c;包含了几个有用的工具和示例。它利用了扩展的BPF&#xff08;Berkeley Packet Filters&#x…

【C/C++】【学生成绩管理系统】深度剖析

可接各类C/C管理系统课设 目录 实现功能 部分1&#xff1a;系统设置和主菜单 1. 引入头文件 2. 定义结构体 3. 函数声明 4. 主函数 部分2&#xff1a;添加学生信息 部分3&#xff1a;删除学生信息 部分4&#xff1a;修改学生信息 部分5&#xff1a;查询学生信息 部分…

大众点评_token,mtgsig

声明 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 本文章未经许可禁止转载&#xff0…

以太网基础知识(三)—FEC概念以及编码原理介绍

1&#xff1a;前言 KR4(528,514) FEC表示的意思&#xff1a; KR4 RS(528,514) FEC 是一种以太网中使用的FEC&#xff08;Forward Error Correction&#xff09;编码方案。在这个方案中&#xff0c;采用的是Reed-Solomon (RS) 编码算法&#xff0c;它被设计用于提高数据传输的…

9M高速USB转接芯片CH347转双串口转I2C转SPI转JTAG转SWD

1、概述 CH347 TSSOP20封装和丝印 CH347 是一款高速 USB 总线转接芯片&#xff0c;通过 USB 总线提供异步串口、I2C 同步串行接口、SPI 同步串行接口和 JTAG 接口等。 在异步串口方式下&#xff0c;CH347 提供了 2 个高速串口&#xff0c;支持 RS485 串口收发使能控制、硬件流控…

论文阅读:基于谱分析的全新早停策略

来自JMLR的一篇论文&#xff0c;https://www.jmlr.org/papers/volume24/21-1441/21-1441.pdf 这篇文章试图通过分析模型权重矩阵的频谱来解释模型&#xff0c;并在此基础上提出了一种用于早停的频谱标准。 1&#xff0c;分类难度对权重矩阵谱的影响 1.1 相关研究 在最近针对…

SN74HC14+陶瓷振子做振荡器的试验初步

本想试验一下465khz用SN74HC14做振荡器&#xff0c;实验了很多次&#xff0c;无法起振。 用1M&#xff0c;4M的也无法起振&#xff0c;用到10Mhz时&#xff0c;能起振&#xff0c;用小频谱仪看&#xff0c;谐波相当丰富&#xff0c;从10M到300Mhz&#xff0c;当然我是通过实验在…

python14 字典类型

字典类型 键值对方式&#xff0c;可变数据类型&#xff0c;所以有增删改功能 声明方式1 {} 大括号&#xff0c;示例 d {key1 : value1, key2 : value2, key3 : value3 ....} 声明方式2 使用内置函数 dict() 创建1)通过映射函数创建字典zip(list1,list2) 继承了序列的所有操作 …

第零篇——数学到底应该怎么学?

目录 一、背景介绍二、思路&方案三、过程1.思维导图2.文章中经典的句子理解3.学习之后对于投资市场的理解4.通过这篇文章结合我知道的东西我能想到什么&#xff1f; 四、总结五、升华 一、背景介绍 宏观讲解数学定位&#xff0c;数学学习方式方法&#xff0c;再次详细学习…

C# OpenCV 部署RecRecNet广角图像畸变矫正

C# OpenCV 部署RecRecNet广角图像畸变矫正 目录 说明 效果 模型信息 项目 代码 下载 说明 ICCV2023 - RecRecNet: Rectangling Rectified Wide-Angle Images by Thin-Plate Spline Model and DoF-based Curriculum Learning 参考&#xff1a; https://github.com/Kang…

Vue52-scoped样式

一、scoped样式的作用 1-1、scoped样式的作用 vue中组件的样式都是汇总到一起的。容易出现一个问题&#xff1a;类名冲突。 示例&#xff1a; school和student组件的类名都叫demo&#xff0c;则student的样式将覆盖school的样式&#xff0c;因为App.vue中&#xff0c;先引入的…

Spring事务管理、SpringAop

目录 ​编辑 Spring事务管理 注解:Transactional rollbackFor 事务属性-传播行为 propagation SpringAOP AOP核心概念 通知类型 通知顺序 切入点表达式 切入点表达式-execution 切入点表达式-annotation 连接点 ​编辑 将案例中 增、删、改 相关接口的操作日志记…