基础系列(六):I/O重定向


1、什么是I/O重定向

    在说I/O重定向之前,先来说说数据流的概念吧。数据流分为三种:标准输入(stdin),标准输出(stdout)和标准错误输出(stderr)。简单来说,标准输入指的是通过键盘键入的信息,标准输出指的是命令执行所回传的正确信息,而标准错误输出指的是命令执行失败后,所回传的错误信息。而I/O重定向就是指修改这些默认的I/O,对原来系统命令的默认执行方式进行改变,通过重定向操作符将命令输入和输出数据流定向到其他位置。




2、I/O重定向的实现

    <1>输出重定向的描述符        

描述符

输出重定向

COMMAND > stdout

stdout作为标准输出(覆盖)

COMMAND >> stdout

stdout作为标准输出(追加)

COMMAND 2> stderr

stderr作为错误输出(覆盖)

COMMAND 2>> stdout 

stderr作为错误输出(追加)

COMMAND &>   

标准输出和输出重定向至新文件(覆盖)

COMMAND &>>

标准输出和输出重定向至新文件(追加)

    <2>输入重定向的描述符  

描述符

输入重定向

COMMAND < stdin > stdout

COMMANDstdin作为标准输入,stdout作为标准输出

COMMAND < stdin

COMMANDstdin作为标准输入

    <3>标准输出重定向代码详解  

       输入一段信息重定向

[root@centos7 ~]# echo hello > file1
[root@centos7 ~]# cat file1
hello

       再输入一段信息,内容将会被覆盖 

[root@centos7 ~]# echo sky  > file1
[root@centos7 ~]# cat file1
sky

       使用追加输出重定向,内容将不再会被覆盖

[root@centos7 ~]# echo who >> file1
[root@centos7 ~]# cat file1
sky
who
[root@centos7 ~]#

     注:2>、2>>和&>、&>>与上格式相同

 *错误重定向也可以使用2&>1  #####与&>相反

user@centos7 ~]$ l a > file1 2>&1
[user@centos7 ~]$ cat file1 
bash: l: command not found...
[root@centos7 ~]# l b >> file1 2>&1
[root@centos7 ~]# cat file1
bash: l: command not found...
bash: l: command not found...
[root@centos7 ~]# ca file1 | tr '[a-z]' '[A-Z]'
bash: ca: command not found...
Similar commands are::
'ac'
'sa'
[root@centos7 ~]# ca file1 2>&1 | tr '[a-z]' '[A-Z]'
BASH: CA: COMMAND NOT FOUND...
SIMILAR COMMANDS ARE::
'AC'
'SA'

 *同时定向输入和输出文件

[root@centos7 ~]# ls a.txt fdsf > file1 2> file2
[root@centos7 ~]# cat file1
a.txt
[root@centos7 ~]# cat file2
ls: cannot access fdsf: No such file or directory

    <4>标准输入代码重定向详解 

        用<表示命令所要读取的文件

[root@centos7 ~]# echo "first line" > file2
[root@centos7 ~]# tr -d fir < file2
st lne
[root@centos7 ~]#

        先用<表示要读取的文件,再使用>表示定向的文件

[root@centos7 ~]# echo "second line" > file3
[root@centos7 ~]# tr -d second < file3 > file4
[root@centos7 ~]# cat file4
 li

     <5>锁定输出重定向

         set -C:禁止将内容覆盖到原文件,但可追加强制覆盖>|

         set +C:允许覆盖

[root@centos7 user1]# echo "thompson" > file5
[root@centos7 user1]# set -C
[root@centos7 user1]# echo "test" > file5 
-bash: file5: cannot overwrite existing file
[root@centos7 user1]# echo "test" >| file5 
[root@centos7 user1]# set +C
[root@centos7 user1]# echo "newtest" > file5
[root@centos7 user1]# cat file5
newtest



补充:cat利用重定向的三个特殊用法

      cat > FILE:单行重定向(即回车即写入)至FILE

[root@centos7 ~]# cat > file5
pipe
shadow
^C
[root@centos7 ~]# cat file5
pipe
shadow

      cat f1 f2 > f3:多行将多个文件的内容合并成一个

[root@centos6 testdir]# cat > f1
a
^C
[root@centos6 testdir]# cat > f2
b
^C
[root@centos6 testdir]# cat f1 f2 > f3
[root@centos6 testdir]# cat f3
a
b

 下面俩个是特殊用法,要记住

       ▲cat << EOF:输入内容并显示,常用作此处文档

[root@centos7 ~]# cat << EOF
> hello
> welcom to university
> EOF
hello
welcom to university
[root@centos7 ~]#

      cat << EOF > FILE:多行重定向(即退出才写入)至FILE

[root@centos7 ~]# cat << EOF > file5
> i can fly
> EOF
[root@centos7 ~]# cat file5
i can fly
[root@centos7 ~]#




补充mail的用法:

mail发送邮件的方法

<1>直接使用shell当编辑器

[root@centos7~]#mail -s "hello" root@10.1.0.17
welcome to my system
EOT    # Ctrl+d 退出编辑

<2>使用管道发送右键

[root@centos7~]#echo "welcome to my system" | mail -s "hi" root@10.1.0.17
You have mail in /var/spool/mail/root

<3>使用输入重定向发送右键(重定向的必须是文件)

[root@centos7~]#mail -s "ok" root@10.1.0.17 < mail.txt


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值