Linux数据流重定向

一、什么是数据流重导向:

数据流重导向 (redirect) 由字面上的意思来看,好像就是将『数据传导到其他地方去』,没错~数据流重导向就是将某个命令运行后应该要出现在屏幕上的数据, 给他传输到其他的地方,例如文件或者是设备(打印机等.)


我们运行一个命令的时候,这个命令可能会由文件读入数据,经过处理之后,再将数据输出到屏幕上。 在上图当中, standard output 与 standard error output 分别代表『标准输出』与『标准错误输出』, 这两个默认都是输出到屏幕上的!那么什么是标准输出与标准错误输出呢?

①  标准输出(standard output)与标准错误输出(standard error output)

简单的说,标准输出指的是『命令运行所回传的正确的信息』,而标准错误输出可理解为『 命令运行失败后,所回传的错误信息』

数据流重导向可以将 standard output (简称 stdout) 与 standard error output (简称 stderr) 分别传送到其他的文件或装置去,而分别传送所用的特殊字符则如下所示:

  1. 标准输入  (stdin) :代码为 0 ,使用 < 或 << ;
  2. 标准输出  (stdout):代码为 1 ,使用 > 或 >> ;
  3. 标准错误输出(stderr):代码为 2 ,使用 2> 或 2>> ;
例:通过标准输出,将输出信息输出到文件

<pre name="code" class="plain">//通过此命令将 根目录下的文件信息保存到 root目录下的outfile文件,如果文件不存在,则创建
//如果文件存在,则将文件清空,再写入信息 ,那么之前的信息被覆盖
[root@localhost ~]# ll / > /root/outfile    

//通过此命令查看/root/outfile 文件内容
[root@localhost ~]# cat /root/outfile
total 98
dr-xr-xr-x.   2 root root  4096 Dec 17 03:42 bin
dr-xr-xr-x.   5 root root  1024 Dec 15 12:25 boot
drwxr-xr-x.  18 root root  3760 Dec 20 22:45 dev
drwxr-xr-x. 103 root root 12288 Dec 22 03:48 etc
drwxr-xr-x.   3 root root  4096 Dec 15 12:23 home
dr-xr-xr-x.  10 root root  4096 Dec 15 12:20 lib
dr-xr-xr-x.   9 root root 12288 Dec 17 03:42 lib64
drwx------.   2 root root 16384 Dec 15 12:17 lost+found
drwxr-xr-x.   2 root root  4096 Sep 23  2011 media
drwxr-xr-x.   3 root root  4096 Dec 15 12:25 mnt
drwxr-xr-x.   3 root root  4096 Dec 15 04:26 opt
dr-xr-xr-x. 199 root root     0 Dec 21 11:33 proc
dr-xr-x---.  27 root root  4096 Dec 24 01:39 root
dr-xr-xr-x.   2 root root 12288 Dec 22 03:48 sbin
drwxr-xr-x.   7 root root     0 Dec 21 11:33 selinux
drwxr-xr-x.   2 root root  4096 Sep 23  2011 srv
drwxr-xr-x.  13 root root     0 Dec 21 11:33 sys
drwxrwxrwt.  34 root root  4096 Dec 23 03:39 tmp
drwxr-xr-x.  16 root root  4096 Dec 21 01:23 usr
drwxr-xr-x.  21 root root  4096 Dec 15 12:21 var

//与上面文件内信息对比,信息是一致的
[root@localhost ~]# ll /
total 98
dr-xr-xr-x.   2 root root  4096 Dec 17 03:42 bin
dr-xr-xr-x.   5 root root  1024 Dec 15 12:25 boot
drwxr-xr-x.  18 root root  3760 Dec 20 22:45 dev
drwxr-xr-x. 103 root root 12288 Dec 22 03:48 etc
drwxr-xr-x.   3 root root  4096 Dec 15 12:23 home
dr-xr-xr-x.  10 root root  4096 Dec 15 12:20 lib
dr-xr-xr-x.   9 root root 12288 Dec 17 03:42 lib64
drwx------.   2 root root 16384 Dec 15 12:17 lost+found
drwxr-xr-x.   2 root root  4096 Sep 23  2011 media
drwxr-xr-x.   3 root root  4096 Dec 15 12:25 mnt
drwxr-xr-x.   3 root root  4096 Dec 15 04:26 opt
dr-xr-xr-x. 199 root root     0 Dec 21 11:33 proc
dr-xr-x---.  27 root root  4096 Dec 23 22:32 root
dr-xr-xr-x.   2 root root 12288 Dec 22 03:48 sbin
drwxr-xr-x.   7 root root     0 Dec 21 11:33 selinux
drwxr-xr-x.   2 root root  4096 Sep 23  2011 srv
drwxr-xr-x.  13 root root     0 Dec 21 11:33 sys
drwxrwxrwt.  34 root root  4096 Dec 23 03:39 tmp
drwxr-xr-x.  16 root root  4096 Dec 21 01:23 usr
drwxr-xr-x.  21 root root  4096 Dec 15 12:21 var
 


所以,使用>来输出信息到文件会覆盖之前的内容,那如果我想要将数据累加而不想要将旧的数据删除,那怎么办?利用两个大于的符号 (>>) 就好啦!以上面的范例来说,你应该要改成『
ll / >>  /root/outtfile 』即可

上面说到的是 standard output 的正确数据,那如果是 standard error output 的错误数据呢?那就通过 2> 及 2>> 来实现!同样是覆盖 (2>) 与累加 (2>>) 的特性!我们在刚刚才谈到 stdout 代码是 1 而 stderr 代码是 2 , 所以这个 2> 是很容易理解的,而如果仅存在 > 时,则代表默认的代码 1 !也就是说:

  • 1> :以覆盖的方法将『正确的数据』输出到指定的文件或装置上;
  • 1>>:以累加的方法将『正确的数据』输出到指定的文件或装置上;
  • 2> :以覆盖的方法将『错误的数据』输出到指定的文件或装置上;
  • 2>>:以累加的方法将『错误的数据』输出到指定的文件或装置上;

例:将正确信息和错误信息分别保存到不同的文件

[root@localhost ~]# su - seth
[seth@localhost ~]$ find /home -name .bashsc > rightfile 2> errorfile
[seth@localhost ~]$ cat /home/seth/rightfile
[seth@localhost ~]$ cat /home/seth/errorfile
find: `/home/elgin': Permission denied

②  标准输入 (standard input): <  与  <<

将原本需要由键盘输入的数据,改由文件内容来取代

键盘输入示例:利用 cat命令创建一个文件

[root@localhost ~]# cat > /root/catfile
test
test create file
haha
[root@localhost ~]# cat /root/catfile
test
test create file
haha

使用标准输入取代键盘输入创建文件:

[root@localhost ~]# cat > catfile < ~/.bashrc
[root@localhost ~]# cat /root/catfile
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
上述的例子介绍了 <  号的作用,那么 << 呢

例:

[root@localhost ~]# cat > catfile << "end"
> this is atest
> hello
> how are you
> end
[root@localhost ~]# cat catfile
this is atest
hello
how are you
利用 << 右侧的控制字符,我们可以终止一次输入, 而不必输入 [crtl]+d 来结束
二、命令运行的依据: ; 与 && 与 ||

在某些情况下,很多命令我想要一次输入去运行,而不想要分次运行时,该如何是好?基本上你有两个选择, 一个是通过 shell script 撰写脚本去运行,一种是通过底下的介绍来一次输入多重命令!

①  cmd;cmd

在命令与命令中间利用分号 (;) 来隔开,这样一来,分号前的命令运行完后就会立刻接着运行后面的命令了。

例:

[root@www ~]# sync; sync; shutdown -h now

②  $? (命令回传值) 与 && 或 ||

命令下达情况 说明
cmd1 && cmd2 1. 若 cmd1 运行完毕且正确运行($?=0),则开始运行 cmd2。
2. 若 cmd1 运行完毕且为错误 ($?≠0),则 cmd2 不运行。
cmd1 || cmd2 1. 若 cmd1 运行完毕且正确运行($?=0),则 cmd2 不运行。
2. 若 cmd1 运行完毕且为错误 ($?≠0),则开始运行 cmd2。
例一:使用 ls 查阅目录 /tmp/abc 是否存在,若存在则用 touch 创建 /tmp/abc/hehe

[root@localhost ~]# ls /tmp/abc && touch /tmp/abc/hehe
ls: cannot access /tmp/abc: No such file or directory
<span class="term_say">很干脆的说明找不到该目录,但并没有 touch 的错误,表示 touch 并没有运行</span> 

例二:测试 /tmp/abc 是否存在,若不存在则予以创建,若存在就不作任何事情

[root@localhost ~]# ls /tmp/abc || mkdir /tmp/abc
ls: cannot access /tmp/abc: No such file or directory     -->确实不存在abc目录,所以执行创建
[root@localhost ~]# ll /tmp/abc
total 0                                                   -->创建成功 说明 mkdir已执行


原文地址:https://www.cnblogs.com/elgin-seth/p/5293731.html