Linux Bash Shell学习(十七):I/O重定向
2010-01-16 22:47
531 查看
本文也即《Learning the bash Shell》3rd Edition的第七章Input/Output and Command-Line Processing之读书笔记之一。我们曾经学习过shell的基本IO重定向操作:>、<和|。基本上能满足95%的情况,但是我们需要知道bash支持的重定向操作。
cmd1
|cmd2
: pipe,将cmd1
的标准输出作为cmd2
的标准输入
>file
:将标准输出重定向到file
<file
:将file作为标准输入
>>file
:将标准输出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆盖文件
当cat不带参数的时候,表示使用标准输入作为输入,这允许在标准输入中键入相关的内容,下面将alias加入.bashrc作为最后一行
$ cat >> .bashrc
alias cdmnt='mount -t iso9660 /dev/sbpcd /cdrom'
^D
>|file
:强制将标准输出重定向到file,即使noclobber设置。当设置环境变量set –o noclobber,将禁止重定向到一个已经存在的文件中,避免文件被覆盖。
n
>|file
:强制将文件描述符n重定向到file,即使noclobber打开
<>file
:将file作为标准输入和标准输出。通常用于设备文件(/dev下面的文件),底层系统程序员可用之测试设备驱动,其他的很少用。
n
<>file
:将file作为文件描述符n的输入和输出
<<label
:Here-document; see text 。将shell的标准输入作为命令的输入,直到行中之包含label。这中间的输入成为here-document。下面是一个例子。我们让人使用cat >> file的方式,通过标准输入在文件中附加内容。
$ cat >> msgfile << . #这里<<.表明以.为结束。因此无需使用^D,而改用.
> this is the text of
> our message.
> .
#这里表示结束。则msgfile中增加了两行this is…和our message.
MACHINE="i586"
OS="linux-gnu"
CC="gcc"
cat > $file <
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF
查看:cat $file,这里给出正常结构
Machine: i586
OS: linux-gnu
Compiler: gcc
如果在EOF加上单引号,或者双引号如下
cat > $file <<'EOF'
则不解析$CC的内容,文件内容如下
Machine: $MACHINE
OS: $OS
Compiler: $CC
如果使用<<-,如下,则删除所有行中前面打头的> tab<这样在脚本的书写上会比较适合阅读
,例如上面的例子可以写为:
cat > $file <<-EOF
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF
n>file
:将文件描述符n重定向到file
n
:将file作为文件描述符的输入
n>>file
:将文件描述符n的输出重定向到file,如果file存在,将输出append到文件后面
n>&
:将标准输出复制到文件描述符n(Duplicate standard output to file descriptor n)
n<&
:从文件描述符n复制标准输入(Duplicate standard input from file descriptor n)
n>&m
:文件描述字n将一个copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
n<&m
:文件描述字n作为文件描述字m中的一个拷贝(File descriptor n is made to be a copy of the input file descriptor)
&>file
: 将标准输出和标准错误输出定向至文件file
<&-
: 关闭标准输入
>&-
: 关闭标准输出
n>&-
: 关闭文件描述字作为输出(Close the output from file descriptor n)
n<&-
:关闭文件描述字作输入(Close the input from file descriptor n)
n>&word:
If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
n<&word
: If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
n>&digit-
: Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
n<&digit-
: Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
文件描述符
文件描述符在bash中比较少用,从0开始用户表示进行的数据流,0表示标准输入,1表示标准输出,2表示标注错误输出,其他从3开始。最为常用的场景是将错误消息输出到某个文件,可以加上2>file
到我们的命令中。
我们来看下面一个脚本的例子:
command
> logfile 2>&1 &
>logfile,表示command的标准输出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的标准错误输出)将copy一份采用文件描述字1(即标准输出),由于标准输出已经重定向logfile,这份copy也见将重定向至文件lofgile。我们可以用“abcd > logfile 2>&1 &”来验证这个效果。最后&表示后台运行的方式。这样命令表示在后台运行command,而它的标准输出和错误输出均重定向到logfile文件中。下面可达到类似的效果:
command
2>&1 | tee logfile &
错误输出同样适用标准输出,通过pipe方式,见他们作为输入执行tee logfile。tee命令将它的标准输入copy至他的标准标准输出以及参数所带的文件中。和上面的命令不一眼这里即会在stdout
和logfile中同时输出。
其他文件描述字的重定向,例如<&n,通常用于从多个文件中读入或者写出。
<&-
,表示强制关闭标准输入
>&-
,表示强制关闭标准输出
1>
,等同于>
0<
,等同于<
相关链接: 我的Linux操作相关文章
cmd1
|cmd2
: pipe,将cmd1
的标准输出作为cmd2
的标准输入
>file
:将标准输出重定向到file
<file
:将file作为标准输入
>>file
:将标准输出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆盖文件
当cat不带参数的时候,表示使用标准输入作为输入,这允许在标准输入中键入相关的内容,下面将alias加入.bashrc作为最后一行
$ cat >> .bashrc
alias cdmnt='mount -t iso9660 /dev/sbpcd /cdrom'
^D
>|file
:强制将标准输出重定向到file,即使noclobber设置。当设置环境变量set –o noclobber,将禁止重定向到一个已经存在的文件中,避免文件被覆盖。
n
>|file
:强制将文件描述符n重定向到file,即使noclobber打开
<>file
:将file作为标准输入和标准输出。通常用于设备文件(/dev下面的文件),底层系统程序员可用之测试设备驱动,其他的很少用。
n
<>file
:将file作为文件描述符n的输入和输出
<<label
:Here-document; see text 。将shell的标准输入作为命令的输入,直到行中之包含label。这中间的输入成为here-document。下面是一个例子。我们让人使用cat >> file的方式,通过标准输入在文件中附加内容。
$ cat >> msgfile << . #这里<<.表明以.为结束。因此无需使用^D,而改用.
> this is the text of
> our message.
> .
#这里表示结束。则msgfile中增加了两行this is…和our message.
MACHINE="i586"
OS="linux-gnu"
CC="gcc"
cat > $file <
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF
查看:cat $file,这里给出正常结构
Machine: i586
OS: linux-gnu
Compiler: gcc
如果在EOF加上单引号,或者双引号如下
cat > $file <<'EOF'
则不解析$CC的内容,文件内容如下
Machine: $MACHINE
OS: $OS
Compiler: $CC
如果使用<<-,如下,则删除所有行中前面打头的> tab<这样在脚本的书写上会比较适合阅读
,例如上面的例子可以写为:
cat > $file <<-EOF
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF
n>file
:将文件描述符n重定向到file
n
:将file作为文件描述符的输入
n>>file
:将文件描述符n的输出重定向到file,如果file存在,将输出append到文件后面
n>&
:将标准输出复制到文件描述符n(Duplicate standard output to file descriptor n)
n<&
:从文件描述符n复制标准输入(Duplicate standard input from file descriptor n)
n>&m
:文件描述字n将一个copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
n<&m
:文件描述字n作为文件描述字m中的一个拷贝(File descriptor n is made to be a copy of the input file descriptor)
&>file
: 将标准输出和标准错误输出定向至文件file
<&-
: 关闭标准输入
>&-
: 关闭标准输出
n>&-
: 关闭文件描述字作为输出(Close the output from file descriptor n)
n<&-
:关闭文件描述字作输入(Close the input from file descriptor n)
n>&word:
If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
n<&word
: If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
n>&digit-
: Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
n<&digit-
: Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
文件描述符
文件描述符在bash中比较少用,从0开始用户表示进行的数据流,0表示标准输入,1表示标准输出,2表示标注错误输出,其他从3开始。最为常用的场景是将错误消息输出到某个文件,可以加上2>file
到我们的命令中。
我们来看下面一个脚本的例子:
command
> logfile 2>&1 &
>logfile,表示command的标准输出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的标准错误输出)将copy一份采用文件描述字1(即标准输出),由于标准输出已经重定向logfile,这份copy也见将重定向至文件lofgile。我们可以用“abcd > logfile 2>&1 &”来验证这个效果。最后&表示后台运行的方式。这样命令表示在后台运行command,而它的标准输出和错误输出均重定向到logfile文件中。下面可达到类似的效果:
command
2>&1 | tee logfile &
错误输出同样适用标准输出,通过pipe方式,见他们作为输入执行tee logfile。tee命令将它的标准输入copy至他的标准标准输出以及参数所带的文件中。和上面的命令不一眼这里即会在stdout
和logfile中同时输出。
其他文件描述字的重定向,例如<&n,通常用于从多个文件中读入或者写出。
<&-
,表示强制关闭标准输入
>&-
,表示强制关闭标准输出
1>
,等同于>
0<
,等同于<
相关链接: 我的Linux操作相关文章
相关文章推荐
- Linux学习日记 —— 10.3.3 Shell基础-Bash基本功能-输入输出重定向
- linux学习第二十三篇:shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
- Linux学习命令汇总四——Bash中变量.文件通配符.管道与重定向.算术运算及循环或判断语句
- Linux Bash Shell学习(九):流程控制——if/else
- Linux学习 -- Shell基础 -- Bash基本功能
- Linux 学习笔记 -- 第三部分 学习 shell 与 shell script -- 第11章 认识与学习 bash
- Linux Bash Shell学习(六):设置环境参数
- linux的shell(bash)脚本学习
- Linux学习笔记 认识bash这个shell
- Linux 学习记录 四(Bash 和 Shell scirpt).
- Linux 学习之路:认识shell和bash
- Linux Bash Shell学习(一):开始看书《Learning the bash Shell,3rd》
- Linux Bash Shell学习(三):基本I/O操作、特殊字符、控制键和帮助
- 【Linux基础学习之三】Bash的一些基础知识总结(通配符、重定向、管道、命令补全)
- linux学习--shell重定向
- Linux之Shell学习(十)Shell输入输出重定向、文件包含
- Linux学习日记 —— 10.5.1 Shell基础-Bash变量-数值运算与运算符
- Linux Bash Shell学习(四):编辑命令
- Linux Bash Shell学习笔记
- Linux Bash Shell学习(二):目录和通配符