重定向分为输出重定向和输入重定向。
一、输出重定向
1、简介
输出重定向分为正确输出和错误输出,这是由FD来控制的,由上一章文章讲到1为标准正确输出,2为标准错误输出。
正确输出命令:程序命令 1> 指定文件 等价于 程序命令 > 指定文件
程序命令 1>> 指定文件 等价于 程序命令 >> 指定文件
错误输出命令:程序命令 2> 指定文件
程序命令 2>> 指定文件
注意:>是覆盖输出,>>是追加输出。
1)演示正确输出
[root@localhost ~]# ls /test //普通查看/test的内容
dir001 dir002 file1 file2 t1 t2 t3 time.txt
//将/test文件夹内容输出到time.txt中,在终端不会显示任何信息。
[root@localhost ~]# ls /test 1> /test/time.txt
//查看time.txt文件,发现ls /test命令输出的内容到了time文件中
[root@localhost ~]# cat /test/time.txt
dir001
dir002
file1
file2
t1
t2
t3
time.txt
tme.txt
//将没有输出信息的程序输出到time文件中
[root@localhost ~]# mkdir /test/list 1> /test/time.txt
//time文件中没有任何信息
[root@localhost ~]# cat /test/time.txt
//将有输出的程序输出的信息放入到time文件中
[root@localhost ~]# mkdir -v /test/list1 1> /test/time.txt
//time中有内容,发现覆盖了原来time文件中的内容
[root@localhost ~]# cat /test/time.txt
mkdir: 已创建目录 "/test/list1"
//将date命令的信息追加到time文件中
[root@localhost ~]# date 1>> /test/time.txt
//发现原来的内容还在,并且多了刚刚写入的信息
[root@localhost ~]# cat /test/time.txt
mkdir: 已创建目录 "/test/list1"
2022年 10月 12日 星期三 14:46:08 CST
注意:程序本身要有输出才可以将输出信息写入指定文件
2)演示错误输出
//将ls/ text的错误信息写入到time中
[root@localhost ~]# ls /test 2> /test/time.txt
//在终端显示出来了
dir001 dir002 file1 file2 list list1 t1 t2 t3 time.txt tme.txt
//time文件中没有信息,因为ls /test命令没有错误信息
[root@localhost ~]# cat /test/time.txt
注意:错误输出是要将由错误信息的程序写入到指定文件,没有错误信息的将写入空信息。
//将ls /aaaaaaaaaaaa 输出的错误信息写入到time文件中
[root@localhost ~]# ls /aaaaaaaaaaaa 2> /test/time.txt
//信息被写入到time文件中
[root@localhost ~]# cat /test/time.txt
ls: 无法访问/aaaaaaaaaaaa: 没有那个文件或目录
3)将正确输出和错误输出都放到一个文件中或者分别放到两个文件中
命令:&>
注意:这个&虽然和将程序放到后台运行时一样的但是&>是表示and的意思中间不能有空格
//查看test目录下的内容,查看aaaaaaaa目录下的内容
[root@localhost ~]# ls /test/ /aaaaaaaa
ls: 无法访问/aaaaaaaa: 没有那个文件或目录
/test/:
dir001 file1 list t1 t3 tme.txt
dir002 file2 rigth t2 time.txt wrong
//将两个命令输出的内容都写入到list文件中(不区分是正确输出还是错误输出)
[root@localhost ~]# ls /test/ /aaaaaaaa &> /test/list
//写入成功
[root@localhost ~]# cat /test/list
ls: 无法访问/aaaaaaaa: 没有那个文件或目录
/test/:
dir001
dir002
file1
file2
list
rigth
t1
t2
t3
time.txt
tme.txt
wrong
//将正确输出写入到right文件中,将错误输出写入到wrong文件中
[root@localhost ~]# ls /test/ 1> /test/right /aaaaaa 2> /test/wrong
//查看right文件,写入成功
[root@localhost ~]# cat /test/right
/test/:
dir001
dir002
file1
file2
list
right
rigth
t1
t2
t3
time.txt
tme.txt
wrong
//查看wrong文件,写入成功
[root@localhost ~]# cat /test/wrong
ls: 无法访问/aaaaaa: 没有那个文件或目录
4)在生产环境中的使用
生产环境中我们一般将信息都写入到/dev/null
/dev/null相当于垃圾桶,表示我们不看这些输出信息,因为有些命令会输出很多行信息,这时候我们只需要写入到/dev/null中即可看不到了。
[root@localhost ~]# ls /test/ /aaaaaaaa &> /dev/null
//不会显示任何信息
[root@localhost ~]#
二、输入重定向
1、简介
标准输入:0< 等价于 <
注意:这的符号和输出符号相反,可以将其看做成是漏斗,信息从大口向小口流入。
2、以输入重定向发送邮件为例
1)默认发邮件过程
发送邮件命令:mail -s “邮件名” 收件人
111 //邮件信息
. //以.为终止查看邮件命令:mail
然后输入邮件号查看邮件
//编辑邮件发送给sure1用户
[root@localhost ~]# mail -s "toyou" sure1
hello. //发现在这一行输入.是不会终止的
. //另起一行输入.会结束编辑
EOT
//切换sure1用户
[root@localhost ~]# su - sure1
上一次登录:三 10月 12 15:23:16 CST 2022pts/0 上
//查看邮件
[sure1@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/sure1": 2 messages 2 new
>N 1 root Wed Oct 12 15:18 18/609 "toyou"
N 2 root Wed Oct 12 15:23 19/613 "mymail"
//选择需要查看的邮件
& 1
Message 1:
From root@localhost.localdomain Wed Oct 12 15:18:49 2022
Return-Path: <root@localhost.localdomain>
X-Original-To: sure1
Delivered-To: sure1@localhost.localdomain
Date: Wed, 12 Oct 2022 15:18:49 +0800
To: sure1@localhost.localdomain
Subject: toyou
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
hello.
//按q退出邮件
& q
Held 2 messages in /var/spool/mail/sure1
//退出sure1用户
[sure1@localhost ~]$ exit
登出
解释邮件对应的信息
N 1 root Wed Oct 12 15:18 18/609 "toyou"
1:邮件编号
root:发送人
Wed Oct 12 15:18:时间
"toyou":邮件名
2)用重定向的方法快速发送邮件
准备一个邮件内容
//创建mail文件
[root@localhost ~]# touch /test/mail
//编辑文件内容
[root@localhost ~]# vim /test/mail
//查看mail文件内容
[root@localhost ~]# cat /test/mail
hello i am sure
通过重定向发邮件
原理:利用输入重定向,把文件内容代替人为的输入。
//将mail文件内的内容输入到邮件中,发送给sure1,邮件名为hello
[root@localhost ~]# mail -s "hello" sure1 0< /test/mail
//切换sure1
[root@localhost ~]# su - sure1
上一次登录:三 10月 12 15:32:59 CST 2022pts/0 上
//查看邮件
[sure1@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/sure1": 3 messages 1 new 2 unread
1 root Wed Oct 12 15:18 19/620 "toyou"
U 2 root Wed Oct 12 15:23 20/623 "mymail"
>N 3 root Wed Oct 12 15:33 18/618 "hello" //收到hello邮件
& 3 //选择查看3号邮件
Message 3:
From root@localhost.localdomain Wed Oct 12 15:33:14 2022
Return-Path: <root@localhost.localdomain>
X-Original-To: sure1
Delivered-To: sure1@localhost.localdomain
Date: Wed, 12 Oct 2022 15:33:14 +0800
To: sure1@localhost.localdomain
Subject: hello
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
hello i am sure //邮件信息
& q //退出邮件
Held 3 messages in /var/spool/mail/sure1
您在 /var/spool/mail/sure1 中有邮件
[sure1@localhost ~]$ exit //退出sure1用户
登出
通过这种方式可以将编辑好的文件信息发送给任何人就不需要一遍遍的编辑,只需要拟好模板即可