Working-with-Standard-Input-Output-Errors-Linux

Linux 中的每个进程都提供三个打开的文件(通常称为文件描述符),分别是标准的输入、输出和错误文件。

  • Standard Input 是键盘,抽象为文件,使编写脚本和程序更容易。
  • Standard Output 运行脚本的窗口或终端,抽象为文件,使编写脚本和程序更容易。
  • Standard error 与标准输出相同,运行脚本的窗口或终端。

文件描述符只是一个数字,它指向一个打开的文件。默认情况下,文件描述符 0 指的是标准输入(stdin),文件描述符 1 指标准输出(stdout),文件描述符 2 指标准错误(stderr)。

当您需要访问特定文件时,特别是当您希望将这些文件重定向到其他位置时,这些数字很重要,文件描述符编号从零开始递增。

Redirecting Standard Output

示例如下:

linuxtechi@localhost:~$ cat /proc/cpuinfo > command.txt

使用 more 命令查看输出数据

linuxtechi@localhost:~$ more command.txt 
processor         : 0
vendor_id         : GenuineIntel
cpu family        : 6
model             : 37
model name        : Intel(R) Core(TM) i3 CPU       M 370  @ 2.40GHz
stepping          : 5
microcode         : 0x616
cpu MHz           : 0.000
cache size        : 6144 KB
physical id       : 0
siblings          : 2
core id           : 0
cpu cores         : 2
apicid            : 0
initial apicid    : 0
fpu               : yes
fpu_exception     : yes
cpuid level       : 5
wp                : yes

> 操作符告诉 shell 将命令的输出重定向到给定的文件,如果该文件存在则删除该文件的旧内容并用命令的输出替换它。

Redirecting a Command’s Input

示例如下:

linuxtechi@localhost:~$ wc -l < command.txt
52

wc 命令的输入来自名为 command.txt 的文件。shell 将文件 command.txt 的内容作为 wc 命令的标准输入发送。

Redirecting Standard Error

除了重定向脚本或命令的标准输入和输出外,我们还可以重定向标准错误。尽管在默认情况下,标准错误与 shell 窗口或终端的标准输出位于相同的位置。将 stdout 和 stderr 分开处理是有充分理由的。主要原因是我们可以将一个或多个命令的输出重定向到一个文件,但无法知道是否发生了错误。将 stderr 和 stdout 分开,可以在屏幕上显示错误消息,而输出仍然进入文件。

示例如下:

linuxtechi@localhost:~$ lsash /usr/bin 2> commands-error.txt
linuxtechi@localhost:~$ cat commands-error.txt
No command 'lsash' found, did you mean:
Command 'sash' from package 'sash' (universe)
lsash: command not found

Redirecting both Standard Output & Standard Error

使用 2>&1 语法将标准错误重定向到与标准输出相同的位置。

示例 1 如下:

linuxtechi@localhost:~$ ls /usr/bin > command.txt 2>&1

上面的命令有三个部分

  • ls /usr/bin 运行的命令
  • command.txt 重定向 ls 命令的输出
  • 2>&1 将文件描述符 2 stderr 的输出发送到与文件描述符 1 stdout 相同的位置。

示例 2 如下:

linuxtechi@localhost:~$ ls /usr2222/bin > command.txt 2>&1
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory

注意: 上面的示例假设系统不存在目录“/usr2222/bin”

Redirecting Both stderr & stdout at Once

linuxtechi@localhost:~$ ls /usr2222/bin &> command.txt
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory

上面的命令结果,将 stdout 和 stderr 重定向到名为 command.txt 的文件。

Appending To Files

使用 >> 操作符重定向命令的输出,如果文件存在,则追加输出到文件。

示例如下:

linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ more sysload.txt
11:49:17 up 1:22, 3 users, load average: 0.28, 0.12, 0.11
11:49:28 up 1:22, 3 users, load average: 0.40, 0.15, 0.12
11:49:36 up 1:23, 3 users, load average: 0.33, 0.14, 0.12

Truncating Files

通过 > 操作符,可以使用一种简写语法来截断文件。

linuxtechi@localhost:~$ ls /usr/bin > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 19713 Dec 2 12:18 command.txt
linuxtechi@localhost:~$ > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Dec 2 12:18 command.txt

Sending Output to Nowhere Fast

在某些情况下,你不仅要重定向命令,还想将输出丢弃。那么你可以将输出重定向到“/dev/null” null 文件会消耗发送给它的所有输出,就像是一个黑洞一样。

linuxtechi@localhost:~$ ls /usr/bin > /dev/null

我的开源项目

酷瓜云课堂-开源知识付费解决方案


鸠摩智首席音效师
475 声望9 粉丝

身强体健,龙精虎猛的活着。