linux 输入重定向和输出重定向

输出重定向

linux中程序默认输出的地方称为标准输出,这一般指屏幕,比如我们输入一个指令ll,屏幕上会列出当前目录下的所有文件信息

[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 115 2016 bin
drwxr-xr-x. 2 root root   6 115 2016 etc
drwxr-xr-x. 2 root root   6 115 2016 games
drwxr-xr-x. 2 root root   6 115 2016 include
drwxr-xr-x. 2 root root   6 115 2016 lib
drwxr-xr-x. 2 root root   6 115 2016 lib64
drwxr-xr-x. 2 root root   6 115 2016 libexec
drwxr-xr-x. 2 root root   6 115 2016 sbin
drwxr-xr-x. 5 root root  49 75 2017 share
drwxr-xr-x. 2 root root   6 115 2016 src
drwxrwx---. 5 root root  33 210 19:18 test
drwxr-xr-x. 4 root root 208 78 2017 tools

不过我们可以使用输出重定向符号 > 或者 >>,让这些信息输出到其他地方,如一个文本文件.

[root@localhost local]# ll > l.out //使用了 >
[root@localhost local]# ll
总用量 4
drwxr-xr-x. 2 root root   6 115 2016 bin
drwxr-xr-x. 2 root root   6 115 2016 etc
drwxr-xr-x. 2 root root   6 115 2016 games
drwxr-xr-x. 2 root root   6 115 2016 include
drwxr-xr-x. 2 root root   6 115 2016 lib
drwxr-xr-x. 2 root root   6 115 2016 lib64
drwxr-xr-x. 2 root root   6 115 2016 libexec
-rw-r--r--. 1 root root 632 210 19:58 l.out //可以看到这个多出的文件
drwxr-xr-x. 2 root root   6 115 2016 sbin
drwxr-xr-x. 5 root root  49 75 2017 share
drwxr-xr-x. 2 root root   6 115 2016 src
drwxrwx---. 5 root root  33 210 19:18 test
drwxr-xr-x. 4 root root 208 78 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 210 19:11 1
drwxr-xr-x. 2 root         root          6 210 04:33 2
drwxrwxr-x. 2 lqxsunhan123 lqxsunhan123  6 210 04:39 3
-rw-r--r--. 1 root         root         26 210 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 210 19:11 1
drwxr-xr-x. 2 root         root          6 210 04:33 2
drwxrwxr-x. 2 lqxsunhan123 lqxsunhan123  6 210 04:39 3
-rw-r--r--. 1 root         root         27 210 20:40 new.out
-rw-r--r--. 1 root         root         26 210 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]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值