Linux shell 重定向输入和输出

Linux shell 重定向输入和输出

  • [1. Standard I/O streams](#1. Standard I/O streams)
  • [2. Redirecting to and from the standard file handles (标准文件句柄的重定向)](#2. Redirecting to and from the standard file handles (标准文件句柄的重定向))
    • [2.1. `command > file`](#2.1. command > file)
    • [2.2. `command >> file`](#2.2. command >> file)
    • [2.3. `command 2> file`](#2.3. command 2> file)
    • [2.4. `command 2>> file`](#2.4. command 2>> file)
    • [2.5. `command < file`](#2.5. command < file)
    • [2.6. `command < infile > outfile`](#2.6. command < infile > outfile)
    • [2.7. `command > file 2>&1`](#2.7. command > file 2>&1)
    • [2.8. `command 2>&1 > file`](#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 Descriptor Name Description 操作符
0 stdin standard input (标准输入) <, <<
1 stdout standard output (标准输出) >, >>, 1>, 1>>
2 stderr standard 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)

相关推荐
紫郢剑侠1 年前
Linux shell编程学习笔记34:eval 命令
linux·学习笔记·eval·shell编程·linux shell
紫郢剑侠1 年前
Linux shell编程学习笔记29:shell自带的 脚本调试 选项
linux·学习笔记·shell脚本·linux shell·bash -x·bash -n·bash -v
紫郢剑侠1 年前
Linux shell编程学习笔记27:tputs
linux·学习笔记·linux shell·脚本编程·tput·tput命令·光标
紫郢剑侠1 年前
Linux shell编程学习笔记17:for循环语句
linux·学习笔记·for循环·linux shell·脚本编程·for语句·循环控制语句
紫郢剑侠1 年前
Linux shell编程学习笔记15:定义数组、获取数组元素值和长度
linux·学习笔记·数组·1024程序员节·linux shell·脚本编程·数组下标