想要把多个Linux 命令适当地组合到一起,使其协同工作,以便我们更加高效地处理数据。要做到这一点,就必须搞明白命令的输入重定向和输出重定向的原理。
简而言之,输入重定向
是指把文件或者键盘输入导入到命令中,而输出重定向
则是指把原本要输出到屏幕的数据信息写入到指定文件中。在日常的学习和工作中,相较于输入重定向,我们使用输出重定向的频率更高,所以又将输出重定向分为了标准输出重定向
和错误输出重定向
两种不同的技术,以及清空写入
与追加写入
两种模式。听起来就很玄妙?
- 标准输入重定向(STDIN,文件描述符为0):默认从键盘输入,也可从其他文件或命令中输入。
- 标准输出重定向(STDOUT,文件描述符为1):默认输出到屏幕。
- 错误输出重定向(STDERR,文件描述符为2):默认输出到屏幕。
对于输入重定向来讲,用到的符号及其作用如下表所示:
| 符号 | 作用 |
| --- | --- |
| 命令 < 文件 | 将文件作为命令的标准输入 |
| 命令 << 分界符 | 从标准输入中读入,直到遇见分界符才停止 |
| 命令 < 文件1 > 文件2 | 将文件1 作为命令的标准输入并将标准输出到文件2 |
对于输出重定向来讲,用到的符号及其作用如下表所示:
| 符号 | 作用 |
| --- | --- |
| 命令 > 文件 | 将标准输出重定向到一个文件中(清空原有文件的数据) |
| 命令 2> 文件 | 将错误输出重定向到一个文件中(清空原有文件的数据) |
| 命令 >> 文件 | 将标准输出重定向到一个文件中(追加到原有内容的后面) |
| 命令 2>> 文件 | 将错误输出重定向到一个文件中(追加到原有内容的后面) |
| 命令 >> 文件 2>&1 或 命令 &>> 文件 | 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)|
对于重定向中的标准输出模式,可以省略文件描述符1 不写,而错误输出模式的文件描述符2是必须要写的。我们先来小试牛刀。通过标准输出重定向将man bash 命令原本要输出到屏幕的信息写入到文件readme.txt 中,然后显示readme.txt 文件中的内容。具体命令如下:
[root@lynchj tmp]# man bash > readme.txt
[root@lynchj tmp]# cat readme.txt
BASH(1) General Commands Manual BASH(1)
NAME
bash - GNU Bourne-Again SHell
SYNOPSIS
bash [options] [file]
COPYRIGHT
Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.
DESCRIPTION
Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C
shells (ksh and csh).
Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-confor‐
mant by default.
OPTIONS
All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the
following options when it is invoked:
-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
-i If the -i option is present, the shell is interactive.
-l Make bash act as if it had been invoked as a login shell (see INVOCATION below).
-r If the -r option is present, the shell becomes restricted (see RESTRICTED SHELL below).
-s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters
to be set when invoking an interactive shell.
-D A list of all double-quoted strings preceded by $ is printed on the standard output. These are the strings that are subject to language translation when the current locale is
not C or POSIX. This implies the -n option; no commands will be executed.
[-+]O [shopt_option]
shopt_option is one of the shell options accepted by the shopt builtin (see SHELL BUILTIN COMMANDS below). If shopt_option is present, -O sets the value of that option; +O
unsets it. If shopt_option is not supplied, the names and values of the shell options accepted by shopt are printed on the standard output. If the invocation option is +O,
the output is displayed in a format that may be reused as input.
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to
--.
Bash also interprets a number of multi-character options. These options must appear on the command line before the single-character options to be recognized.
--debugger
Arrange for the debugger profile to be executed before the shell starts. Turns on extended debugging mode (see the description of the extdebug option to the shopt builtin
below).
--dump-po-strings
Equivalent to -D, but the output is in the GNU gettext po (portable object) file format.
--dump-strings
Equivalent to -D.
--help Display a usage message on standard output and exit successfully.
--init-file file
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).
--login
Equivalent to -l.
--noediting
Do not use the GNU readline library to read command lines when the shell is interactive.
………………省略部分输出信息………………
有没有感觉到很方便呢?我们接下来尝试输出重定向技术中的覆盖写入与追加写入这两种不同模式带来的变化。首先通过覆盖写入模式向readme.txt 文件写入一行数据(该文件中包含上一个实验的man 命令信息),然后再通过追加写入模式向文件再写入一次数据,其命令如下:
[root@lynchj tmp]# echo "哈哈,进行追加输出了" >> readme.txt
[root@lynchj tmp]# tail -n 10 readme.txt
cutes the next command in the sequence. It suffices to place the sequence of commands between parentheses to force it into a subshell, which may be stopped as a unit.
Array variables may not (yet) be exported.
There may be only one active coprocess at a time.
GNU Bash-4.2 2010 December 28 BASH(1)
哈哈,进行追加输出了
虽然都是输出重定向技术,但是不同命令的标准输出和错误输出还是有区别的。例如查看当前目录中某个文件的信息,这里以readme.txt 文件为例。因为这个文件是真实存在的,因此使用标准输出即可将原本要输出到屏幕的信息写入到文件中,而错误的输出重定向则依然把信息输出到了屏幕上。
[root@lynchj tmp]# ll readme.txt
-rw-r--r--. 1 root root 284185 May 15 16:53 readme.txt
[root@lynchj tmp]# ll readme.txt > readme.txt
[root@lynchj tmp]# ll readme2.txt > readme.txt
ls: cannot access readme2.txt: No such file or directory
如果想把命令的报错信息写入到文件,该怎么操作呢?当用户在执行一个自动化的Shell脚本时,这个操作会特别有用,而且特别实用,因为它可以把整个脚本执行过程中的报错信息都记录到文件中,便于安装后的排错工作。接下来我们以一个不存在的文件进行实验演示:
[root@lynchj tmp]# ll xxxxxxxxxxxx
ls: cannot access xxxxxxxxxxxx: No such file or directory
[root@lynchj tmp]# ll xxxxxxxxxxxx > readme.txt
ls: cannot access xxxxxxxxxxxx: No such file or directory
[root@lynchj tmp]# ll xxxxxxxxxxxx 2> readme.txt
[root@lynchj tmp]# cat readme.txt
ls: cannot access xxxxxxxxxxxx: No such file or directory
输入重定向相对来说有些冷门,在工作中遇到的概率会小一点。输入重定向的作用是把文件直接导入到命令中。接下来使用输入重定向把readme.txt 文件导入给wc -l 命令,统计一下文件中的内容行数。
[root@lynchj tmp]# wc -m < readme.txt
58
文章评论