马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
0 引言
在前面的学习条记中,我们常常使用echo命令和输出重定向来生成脚本文件或演示文件,其实Linux提供了一个可以从尺度输入读取数据,并输出成文件的命令——tee。
1 tee命令 的帮助信息、功能、命令格式、选项和参数阐明
1.1 tee命令 的帮助信息
我们可以输入命令 tee --help 来查看tee命令的帮助信息。
- [purpleendurer @ bash ~] tee --help
- Usage: tee [OPTION]... [FILE]...
- Copy standard input to each FILE, and also to standard output.
- -a, --append append to the given FILEs, do not overwrite
- -i, --ignore-interrupts ignore interrupt signals
- --help display this help and exit
- --version output version information and exit
- If a FILE is -, copy again to standard output.
- GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
- Report tee translation bugs to <http://translationproject.org/team/>
- For complete documentation, run: info coreutils 'tee invocation'
- [purpleendurer @ bash ~]
复制代码
1.2 tee命令 的功能
tee 是一个简单的命令行实用程序,它接受输入并将输出写入文件和尺度输出(即终端)。
tee命令和输出重定向都可以把输出内容保存到文件,但两者仍旧存在差异:
输出重定向只能保存到1个文件,
tee允许我们把尺度想要将输出重定向到多个文件。
Tee 命令是 GNU coreutils 的一部分,因此它预装在所有 Linux 发行版中。
1.3 tee命令 命令格式
tee [选项]... [文件]...
1.4 tee命令 选项阐明
选项阐明-a, --append附加到给定的 FILE,不覆盖-i, --ignore-interrupts忽略停止信号--help显示此帮助并退出--version输出版本信息并退出 1.5 tee命令 参数阐明
文件:将尺度输入保存到的文件名
2 tee命令使用实例
2.1 将尺度输入内容保存到多个文件
比方,将尺度输入内容"I am purpleendurer"保存到文件 t1 和 t2的命令是:
tee t1 t2
- [purpleendurer @ bash ~] tee t1 t2
- I am purpleendurer
- I am purpleendurer
- [purpleendurer @ bash ~] ls
- Code t1 t2
- [purpleendurer @ bash ~] cat t1
- I am purpleendurer
- [purpleendurer @ bash ~] cat t2
- I am purpleendurer
- [purpleendurer @ bash ~]
复制代码
我们通过尺度输入设备键盘输入 I am purpleendurer 回车并按Ctr+D保存
tee命令默认将尺度输入的内容输出到尺度输出设备,所以我们会看到两行 I am purpleendurer
随后我们使用ls命令查看当看目次,发现了新创建的文件t1和t2
接着我们使用cat命令查看文件t1和t2的内容,两个文件的内容都是我们刚才输入的 I am purpleendurer
2.2 将尺度输入内容追加保存到多个文件,并且不将输入内容显示在尺度输出设备上
我们仍旧以文件t1和t2为例。
- [purpleendurer @ bash ~] cat t1 t2
- I am purplendurer
- I am purplendurer
- [purpleendurer @ bash ~] tee -a t1 t2 > /dev/null
- Linux is good.
- [purpleendurer @ bash ~] cat t1
- I am purplendurer
- Linux is good.
- [purpleendurer @ bash ~] cat t2
- I am purplendurer
- Linux is good.
- [purpleendurer @ bash ~]
复制代码
2.2.1 起首,我们使用cat命令查看文件t1和t2的内容,两个文件的内容都是:
I am purpleendurer
2.2.2 然后,我们使用命令带-a选项的tee命令
tee -a t1 t2 > /dev/null
将尺度输入内容追加到文件t1 和 t2,并使用了
> /dev/null
将尺度输出重定向到 /dev/null,这样tee命令就不会把输入的内容输出到尺度输出设备上。
2.2.3 接着,我们输入:Linux is good 回车并按Ctrl+D保存。
2.2.4 末了,我们使用cat命令查看文件t1和t2的内容,两个文件的内容都是:
I am purpleendurer
Linux is good.
2.3 与echo命令和管道操纵配合使用
- [purpleendurer @ bash ~] echo Hello world | tee t3 t4
- Hello world
- [purpleendurer @ bash ~] cat t3 t4
- Hello world
- Hello world
- [purpleendurer @ bash ~]
复制代码
我们使用echo命令输出字符串Hello world,字符串Hello world经过管道输入到tee 命令,被tee 命令存入文件t3和t4。
使用cat命令查看文件t1和t2的内容,两个文件的内容都是字符串Hello world
所以命令
echo Hello world | tee t3 t4
相称于两条命令
echo Hello world > t3
echo Hello world > t4
通过管道操纵,tee命令不会回显输入的字符串Hello world
2.4 将堕落信息保存到文件
- [purpleendurer @ bash ~] ifconfig | tee i
- bash: ifconfig: command not found
- [purpleendurer @ bash ~] cat i
- [purpleendurer @ bash ~] ifconfig 2>&1 | tee i
- bash: ifconfig: command not found
- [purpleendurer @ bash ~] cat i
- bash: ifconfig: command not found
- [purpleendurer @ bash ~]
复制代码
管道操纵一般转发尺度输出信息(1),不转发命令实行时反馈的堕落信息(2)。
如果我们想将命令实行时反馈的堕落信息也存储下来,可以使用l输出重定向
2>&1
将堕落信息重定向到尺度输出。此中1代表尺度畀出,2代表堕落信息。
在上面的例子里,命令
ifconfig
没有尺度堕落,只有堕落信息:
bash: ifconfig: command not found
所以命令
ifconfig | tee i
中 没有信息通过管道输入给命令tee,也就没有信息存储到文件i
所以我们使用cat命令查看文件i是没有内容的。
当我们在ifconfig命令后面增长了 2>&1,命令变成
ifconfig 2>&1 | tee i
ifconfig命令的堕落信息被重定向到尺度输入,通过管道传输给tee命令并保存到文件1中。
这里我们使用cat命令查看文件i的内容,就是:
bash: ifconfig: command not found
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |