好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

bash重定向详解 - Linux操作系统:Ubuntu_Centos_Debia

bash重定向详解

 

首先我们先回顾下bash现有的重定向符号

 

1.重定向输入输出,目标是文件word

 

[n]>|word   默认n为1    noclobber选项有关,直接例子就明白它的用处了

 

igi@igi-debian:~$ rm -f testfile

igi@igi-debian:~$ touch testfile

igi@igi-debian:~$ cat testfile

igi@igi-debian:~$ set -o noclobber

igi@igi-debian:~$ echo 2 >testfile

bash: testfile: cannot overwrite existing file

igi@igi-debian:~$ echo 2 >| testfile

igi@igi-debian:~$ cat testfile

2.重定向标准错误和标准输出到指定文件描述符

 

&>word      更通用

>&word

追加输出

 

&>>word     没有>>&word的表达方法

3.Here Documents

 

<<[-]word

   here-document

delimiter

-符号将删除所有行开头的tab符

4.Here Strings

 

<<<word

5.复制文件描述符

 

[n]<&word   默认n为0,如果为数字,必须得为打开的文件描述符

[n]<&-      关闭文件描述符

 

[n]>&word   默认n为1,如果为数字,必须得为打开的文件描述符

[n]>&-      关闭文件描述符

6.移动文件描述符

 

7.以读写方式打开文件描述符

 

[n]<>word   文件不在时会被创建

如果要深刻理解重定向,先要明白以下2点

1.shell(bash或者csh等)负责重定向,对于程序或者函数,这一切都是透明的,它只管输入输出,至于从哪输入哪输出,是shell解释器负责

2.shell命令解析过程中,在实际执行命令前,IO重定向先设置好

 

我们来看以下的例子

 

1.’echo 1 a1 >a2′ 与 ‘echo 1 >a2 a1′

 

IO重定向是在命令执行前设置好,所以上面两种情况,最后的效果一样,bash先把输出重定向到a2文件,再执行’echo 1 a1′

 

2.’ls nothisfile >res 2>&1′ 与 ‘ls nothisfile 2>&1 >res’

 

igi@igi-debian:~/rtest$ ls nothisfile

ls: cannot access nothisfile: No such file or directory

igi@igi-debian:~/rtest$ cat res

ls: cannot access nothisfile: No such file or directory

igi@igi-debian:~/rtest$ ls nothisfile 2>&1 >res

ls: cannot access nothisfile: No such file or directory

igi@igi-debian:~/rtest$ cat res

a

b

c

res

igi@igi-debian:~/rtest$ ls -1 2>&1 >res

igi@igi-debian:~/rtest$ cat res

a

b

c

res

‘ls nothisfile >res 2>&1′,文件描述符1被重定向到文件res(本来是标准输出),然后再把文件描述符2重定向到文件描述符1(此时是文件描述符1指向文件res),最后执行]ls nothisfile],产生错误,被送往文件描述符2,最后流向文件res。

‘ls nothisfile 2>&1 >res’,文件描述符2被重定向到文件描述符1(即标准输出:屏幕),然后再把文件描述符1重定向到文件res,结果是文件描述符2被重定向到标准输出,文件描述符1被重定向到文件res,最后执行]ls nothisfile]产生的错误就被送往屏幕。

 

3.’ls nothisfile a >&word’ 与 ‘ls nothisfile a >&123456′

 

a

igi@igi-debian:~/test/shell$ cat a

this is a

igi@igi-debian:~/test/shell$ ls nothisfile a >&word

igi@igi-debian:~/test/shell$ cat word

ls: cannot access nothisfile: No such file or directory

a

-bash: 123456: Bad file descriptor

cat: 123456: No such file or directory

>&这个重定向符号,在前面有提到,[重定向标准错误和标准输出到指定文件描述符[ 和 [复制文件描述符[ 都有这个符号, 实际上[重定向标准错误和标准输出到指定文件描述符[ 是 [复制文件描述符[ 的一种特别情况, 即当 [n]>&word 的n省略 且 word不是数字时, 会重定向标准错误和标准输出 到指定文件。

[ls nothisfile a >&word] , 由于word不是纯数字, bash解析成 ]重定向标准错误和标准输出到指定文件描述符[, 效果相当于 ] ls nothisfile a >word 2>&1″

[ls nothisfile a >&123456″, 由于123456是纯数字, bash解析成 [复制文件描述符], 相当于 ] ls nothisfile a 1>&123456″ , 但由于 [ 复制文件描述符 [规定 [如果为数字,必须得为打开的文件描述符]

所以发生了错误。

 

4.’ls a 1>&-’ 与 ‘ ls a >&1- ‘

 

igi@igi-debian:~/test/shell$ ls

a

igi@igi-debian:~/test/shell$ cat a

this is a

igi@igi-debian:~/test/shell$ ls a >&1-

a

igi@igi-debian:~/test/shell$ ls a 1>&-

ls: write error: Bad file descriptor

[ ls a >&1- [, >&1-属于[移动文件描述符] 提到的 [[n]>&digit-], 用文件描述符digit替换掉文件描述符n, n描述符被关闭.n默认为1. [ls a >&1-], 相当与 ] ls a 1>&1- [, 把文件描述符1替换掉原

文件描述符1, 然后关闭原文件描述符1, 没发生变化, 输出依然被送到屏幕

] ls a 1>&- [, >&- 属于 [复制文件描述符] 提到的]关闭文件描述符], ] ls a 1>&- [, 关闭了文件描述符1, 在运行] ls a], 由于输出默认都送到文件描述符1, 而它被关闭, 报]错误的文件描述符]

 

5.’ls a nothisfile 1>&2-’ 与 ‘ ls a nothisfile 1<&2- '

 

a

igi@igi-debian:~/test/shell$ ls a nothisfile 1<&2-

a

igi@igi-debian:~/test/shell$ ls a nothisfile 1>&2-

a

igi@igi-debian:~/test/shell$ exec 3<>test

igi@igi-debian:~/test/shell$ ls a nothisfile 1>&3-

ls: cannot access nothisfile: No such file or directory

igi@igi-debian:~/test/shell$ cat test

a

igi@igi-debian:~/test/shell$

‘ 1>&2- ‘ 与 ‘ 1<&2- ' 是一个效果的, [n]>&digit- 和 [n]<&digit- 这两个移动文件描述符的操作, 都是移动digit 到 n , 区别在n没有指定时, <&digit- 等于 0<&digit-, 而 >&digit- 等于 1>&digit-

移动文件描述符, 就是把描诉符digit的指向给描述符n, 然后关了digit. 例子中的exec很好的解释移动文件描述符的行为, 文件描述符3被定向到文件test, 然后1>&3-, 会使得1也定向到3所定向的文件test, 然后文件描诉符3被关闭, 效果就是标准输出被定向到test文件.

查看更多关于bash重定向详解 - Linux操作系统:Ubuntu_Centos_Debia的详细内容...

  阅读:59次