数据流重定向

Linux默认提供三个特殊设备,用于终端显示和输出,分别为stdin(标准输入,对应于终端的输入),stdout(标准输出,对应于终端的输出),stderr(标准错误输出,对应于终端输出)。

文件描述符设备文件说明
0/dev/stdin标准输入
1/dev/stdout标准输出
2/dev/stderr标准错误

文件描述符:文件描述符形式上时一个非负整数,实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念只适用于UNIX、Linux这样的操作系统。

一、标准重定向

  • cat的连续输出(heredoc方式)重定向到一个文件:
shiyanlou:~/ $ cat > eldon.c << EOF
heredoc> int mian()
heredoc> {
heredoc>     return 0;
heredoc> }
heredoc> EOF
  • echo命令的输出从默认的标准输出重定向到一个文件:
shiyanlou:~/ $ echo "hello world" > redirect
shiyanlou:~/ $ cat redirect
hello world
shiyanlou:~/ $ echo "hello world1" >> redirect
shiyanlou:~/ $ cat redirect                  
hello world
hello world1
shiyanlou:~/ $ echo "hello world" > redirect
shiyanlou:~/ $ cat redirect                
hello world

注意管道和重定向的区别:管道默认是连接前一个命令的输出到下一个命令的输入,而重定向通常是需要一个文件来建立两个命令的连接。

二、标准错误重定向:

标准输出和标准错误都被指向伪终端的屏幕显示,所以我们经常看到的一个命令的输出通常是同时包含了标准输出和标准错误:

shiyanlou:~/ $ cat eldon.c eldon.log
int mian()
{
    return 0;
} #标准输出
cat: eldon.log: No such file or directory #标准错误

标准错误虽然和标准输出一样都默认指向伪终端屏幕,实际上他们的原理并不一样,读者可以自行领会下面操作的差异:

shiyanlou:~/ $ cat eldon.log > errlog
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log &> errlog
shiyanlou:~/ $ cat errlog
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log > mylog 2>&1
shiyanlou:~/ $ cat mylog\
cat: eldon.log: No such file or directory

注意:需要在标准错误重定向文件描述符前加上&,否则shell会当做重定向到一个文件名为1的文件中。

三、tee命令

可以使用tee命令把输出重定向到多个文件。

shiyanlou:~/ $ echo "eldonzhao" | tee eldon1 eldon2 eldon3
eldonzhao
shiyanlou:~/ $ ls
Code  Desktop  eldon1  eldon2  eldon3

四、永久重定向

我们可以通过exec命令实现“永久”重定向。exec命令的作用是使用指定的命令替换当前的Shell,及使用一个进程替换当前进程,或者指定新的重定向。

shiyanlou:~/ $ zsh #创建一个子Shell
shiyanlou:~/ $ exec 1>eldon.log
shiyanlou:~/ $ ls
shiyanlou:~/ $ exit #退出子Shell
shiyanlou:~/ $ cat eldon.log
Code
Desktop
eldon.log

五、创建输出文件描述符

默认在Shell中可以打开9个文件描述符,系统默认只打开012三个文件描述符,我们可以手动打开3-8的文件描述符。

shiyanlou:~/ $ exec 3>eldon.err[11:13:16]
shiyanlou:~/ $ cd /dev/fd;ls -Al; cd -[11:13:18]
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:12 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 2 -> /dev/pts/0
l-wx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 3 -> /home/shiyanlou/eldon.e
rr
~
shiyanlou:~/ $ echo "this is eldon" >&3
shiyanlou:~/ $ cat eldon.err
this is eldon
shiyanlou:~/ $ exit

六、关闭文件描述符

shiyanlou:~/ $ exec 3>eldon.err
shiyanlou:~/ $ cd /dev/fd;ls -Al;cd -
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 2 -> /dev/pts/0
l-wx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 3 -> /home/shiyanlou/eldon.e
rr
~
shiyanlou:~/ $ exec 3>&-
shiyanlou:~/ $ cd /dev/fd;ls -Al;cd -
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 2 -> /dev/pts/0
~

七、完全屏蔽命令输出

在Linux中有一个被称为“黑洞”的设备文件,所以导入它的数据都将被“吞噬”。

在类UNIX系统中,/dev/null或称为空设备,是一个特殊的设备文件,它通常被用于丢弃不需要的输出流,或作为用于输入流的空文件,这些操作通常由重定向完成。读取它则会得到一个EOF。

shiyanlou:~/ $ cat eldon.log            
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log 1>/dev/null 2>&1

八、使用xargs分割参数列表

xargs是一条UNIX和类UNIX操作系统的常用命令。它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长。

shiyanlou:~/ $ cut -d : -f 1 < /etc/passwd | sort| xargs echo
backup bin daemon games gnats irc libuuid list lp mail man memcache messagebu
s mongodb mysql news nobody proxy redis root shiyanlou sshd sync sys syslog t
omcat7 uucp www-data
    原文作者:EldonZhao
    原文地址: https://www.jianshu.com/p/bc46db89cc9f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞