云计算怎么学?Io重定向Redirection详解

Linux shell中的File Descripter可以理解为一个指向文件的指针。默认有三个FD:0,1,2。分别指向的是:Keyboard设备文件,Moniter设备文件,和Moniter设备文件。Shell中还允许有3--9的FD,默认都没有打开,可以认为指向的为NULL。

可以通过以下命令查看打开的FD:ls /proc/self/fd返回的数字代表FD的值。利用重定向可以为一个FD赋值,使其指向一个非NULL的文件,其实就是打开一个FD。

6>&1

可以理解为将FD6指针指向FD1指针指向的文件,即Moniter。这样,FD6和FD1就同时指向同一个文件:Moniter。6>&-可以理解为将FD6指针置为空值null,即关闭FD6。一个重定向只在当前命令中有效。exec可以使IO重定向在当前shell中长期有效。通过类似指针的特性,I/O重定向可以用来保存当前某个FD并在其后恢复。例如:

过滤stderr中的内容,屏蔽含有字符串“unused”的错误输出:

创建FD6,指向FD1指向的moniter。exec 6>&1# 运行command命令,将stdout重定向给FD6,再将stderr重定向给stdout。# 这时,command有两个输出FD,# 分别是FD1和FD6,FD1是错误信息,FD6是标准输出内容。# 再将FD1错误信息通过pipe传给grep进行过滤,并将过滤后的内容重定向给FD2。# 最后,将之前的FD6的内容重定向给FD1。# 这样,这条命令有两个输出,FD1是command的标准输出内容,# FD2是经过过滤的command的错误输出。(command 2>&1 >&6 | grep -v unused >&2) 6>&1# 关闭FD6。

exec 6>&-

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中同时输出。

猜你喜欢

转载自blog.51cto.com/13694927/2119174