Linux shell编程学习条记84:tee命令——显示保存两不误

打印 上一主题 下一主题

主题 982|帖子 982|积分 2956

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

0 引言

在前面的学习条记中,我们常常使用echo命令和输出重定向来生成脚本文件或演示文件,其实Linux提供了一个可以从尺度输入读取数据,并输出成文件的命令——tee。
1 tee命令 的帮助信息、功能、命令格式、选项和参数阐明

1.1 tee命令 的帮助信息

我们可以输入命令 tee --help 来查看tee命令的帮助信息。
  1. [purpleendurer @ bash ~] tee --help
  2. Usage: tee [OPTION]... [FILE]...
  3. Copy standard input to each FILE, and also to standard output.
  4.   -a, --append              append to the given FILEs, do not overwrite
  5.   -i, --ignore-interrupts   ignore interrupt signals
  6.       --help     display this help and exit
  7.       --version  output version information and exit
  8. If a FILE is -, copy again to standard output.
  9. GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
  10. Report tee translation bugs to <http://translationproject.org/team/>
  11. For complete documentation, run: info coreutils 'tee invocation'
  12. [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
  1. [purpleendurer @ bash ~] tee t1 t2
  2. I am purpleendurer
  3. I am purpleendurer
  4. [purpleendurer @ bash ~] ls
  5. Code  t1  t2
  6. [purpleendurer @ bash ~] cat t1
  7. I am purpleendurer
  8. [purpleendurer @ bash ~] cat t2
  9. I am purpleendurer
  10. [purpleendurer @ bash ~]
复制代码


我们通过尺度输入设备键盘输入 I am purpleendurer 回车并按Ctr+D保存
tee命令默认将尺度输入的内容输出到尺度输出设备,所以我们会看到两行 I am purpleendurer
随后我们使用ls命令查看当看目次,发现了新创建的文件t1和t2
接着我们使用cat命令查看文件t1和t2的内容,两个文件的内容都是我们刚才输入的  I am purpleendurer
2.2 将尺度输入内容追加保存到多个文件,并且不将输入内容显示在尺度输出设备上

我们仍旧以文件t1和t2为例。
  1. [purpleendurer @ bash ~] cat t1 t2
  2. I am purplendurer
  3. I am purplendurer
  4. [purpleendurer @ bash ~] tee -a t1 t2  > /dev/null
  5. Linux is good.
  6. [purpleendurer @ bash ~] cat t1
  7. I am purplendurer
  8. Linux is good.
  9. [purpleendurer @ bash ~] cat t2
  10. I am purplendurer
  11. Linux is good.
  12. [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命令和管道操纵配合使用

  1. [purpleendurer @ bash ~] echo Hello world | tee t3 t4
  2. Hello world
  3. [purpleendurer @ bash ~] cat t3 t4
  4. Hello world
  5. Hello world
  6. [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 将堕落信息保存到文件

  1. [purpleendurer @ bash ~] ifconfig | tee i
  2. bash: ifconfig: command not found
  3. [purpleendurer @ bash ~] cat i
  4. [purpleendurer @ bash ~] ifconfig 2>&1 | tee i
  5. bash: ifconfig: command not found
  6. [purpleendurer @ bash ~] cat i
  7. bash: ifconfig: command not found
  8. [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企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表