输出重定向
linux中程序默认输出的地方称为标准输出,这一般指屏幕,比如我们输入一个指令ll
,屏幕上会列出当前目录下的所有文件信息
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 7月 5 2017 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
drwxrwx---. 5 root root 33 2月 10 19:18 test
drwxr-xr-x. 4 root root 208 7月 8 2017 tools
不过我们可以使用输出重定向符号 > 或者 >>,让这些信息输出到其他地方,如一个文本文件.
[root@localhost local]# ll > l.out //使用了 >
[root@localhost local]# ll
总用量 4
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
-rw-r--r--. 1 root root 632 2月 10 19:58 l.out //可以看到这个多出的文件
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 7月 5 2017 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
drwxrwx---. 5 root root 33 2月 10 19:18 test
drwxr-xr-x. 4 root root 208 7月 8 2017 tools
我们查看l.out的内容,可以发现就是ll时输出的内容。
关于>和>>,这两者有不同,首先这两个符号后面接文件名,若文件不存在,则会创建一个文件,然后写入内容,若文件存在时,>会直接删除掉原文件,然后进行创建写入,而 >> 会在当前文件末尾进行追加。我们就不演示了
输入重定向
标准输入一般指键盘输入,cat命令用于将指定输入输出到标准输出,即屏幕上,我们一般用cat查看文件内容,若直接输入cat,则会接受键盘的输入,直到遇到ctrl + d结束符
[root@localhost local]# cat
hello
hello
I accept anything
I accept anything
^C
[root@localhost local]#
我们还可以把cat的输入重定向到文件内容,则可以达到查看文件内容的效果。符号是 < 或者 <<
[root@localhost test]# ll
总用量 4
drwxr-xr-x. 3 root root 16 2月 10 19:11 1
drwxr-xr-x. 2 root root 6 2月 10 04:33 2
drwxrwxr-x. 2 lqxsunhan123 lqxsunhan123 6 2月 10 04:39 3
-rw-r--r--. 1 root root 26 2月 10 20:23 text.out
[root@localhost test]# cat < text.out // 把text.out的内容输入到cat中然后输出到标准输出
this is a txt
content....
[root@localhost test]#
不过一般我们查看文件内容直接 cat 文件名 就行.>>符号我们下面讲解.
输入重定向与输出重定向配合(创建文本文件)
我们还可以将以上两种重定向配合起来以达到创建文件的效果
[root@localhost test]# cat > new.out // 这个命令首先使用cat接受键盘输入,将输入结果重定向到new.out中输出,ctrl+d结束
input in this file
aaa
bbb
[root@localhost test]# ll
总用量 8
drwxr-xr-x. 3 root root 16 2月 10 19:11 1
drwxr-xr-x. 2 root root 6 2月 10 04:33 2
drwxrwxr-x. 2 lqxsunhan123 lqxsunhan123 6 2月 10 04:39 3
-rw-r--r--. 1 root root 27 2月 10 20:40 new.out
-rw-r--r--. 1 root root 26 2月 10 20:23 text.out
[root@localhost test]# cat new.out //查看内容,发现即是刚刚输入的内容
input in this file
aaa
bbb
[root@localhost test]#
这样一行一行的输入如果不方便,我们使用 << 来达到一个效果,这个符号是立即文档,他接受键盘的输入,并可以指定一个结束符,然后再输出到指定输出去。
我们下面在刚刚的new.out里追加内容
[root@localhost test]# cat new.out //查看new.out内容
input in this file
aaa
bbb
[root@localhost test]# cat << END >> new.out //追加内容
> this is append content!!!
> I can't end unless I meet END
> END // 这是指定的结束符
[root@localhost test]# cat new.out //再次查看内容
input in this file
aaa
bbb
this is append content!!!
I can't end unless I meet END
[root@localhost test]#