用途说明
   在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls
>a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令了。tee命令读取标准输入,把这些内容同时输出到标准输出和(多个)文件中(read
from standard input and write to standard output and files. Copy standard input
to each FILE, and also to standard output. If a FILE is -, copy again to
standard output.)。在info tee中说道:tee命令可以重定向标准输出到多个文件(`tee': Redirect output to
multiple files. The `tee' command copies standard input to standard output and
also to any files given as arguments.  This is useful when you want not only to
send some data down a pipe, but also to save a
copy.)。要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取。
   
   常用参数
  
格式:
tee
   只输出到标准输出,因为没有指定文件嘛。
   
   格式:tee
file
   输出到标准输出的同时,保存到文件file中。如果文件不存在,则创建;如果已经存在,则覆盖之。(If a file being
written to does not already exist, it is created. If a file being written to
already exists, the data it previously
   contained is overwritten unless the
`-a' option is used.)
   
   格式:tee -a file
  
输出到标准输出的同时,追加到文件file中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。
   
  
格式:tee
-
   输出到标准输出两次。(A FILE of `-' causes `tee' to send another
copy of input to standard output, but this is typically not that useful as the
copies are interleaved.)
   
   格式:tee file1 file2
-
   输出到标准输出两次,同时保存到file1和file2中。
   
   使用示例
   示例一
tee命令与重定向的对比
   [root@web ~]# seq 5 >1.txt
   [root@web ~]#
cat 1.txt
   1
   2
   3
   4
   5
   [root@web ~]# cat 1.txt
>2.txt
   [root@web ~]# cat 1.txt | tee 3.txt
   1
   2
  
3
   4
   5
   [root@web ~]# cat 2.txt
   1
   2
   3
  
4
   5
   [root@web ~]# cat 3.txt
   1
   2
   3
   4
  
5
   [root@web ~]# cat 1.txt >>2.txt
   [root@web ~]# cat 1.txt |
tee -a 3.txt
   1
   2
   3
   4
   5
   [root@web ~]# cat
2.txt
   1
   2
   3
   4
   5
   1
   2
   3
  
4
   5
   [root@web ~]# cat 3.txt
   1
   2
   3
   4
  
5
   1
   2
   3
   4
   5
   [root@web ~]#

示例二 使用tee命令重复输出字符串
   [root@web ~]# echo 12345 | tee
  
12345
   [root@web ~]# echo 12345 | tee -
   12345
   12345
  
[root@web ~]# echo 12345 | tee - -
   12345
   12345
   12345
  
[root@web ~]# echo 12345 | tee - - -
   12345
   12345
   12345
  
12345
   [root@web ~]# echo 12345 | tee - - - -
   12345
  
12345
   12345
   12345
   12345
   [root@web ~]#
   [root@web
~]# echo -n 12345 | tee
   12345[root@web ~]# echo -n 12345 | tee -
  
1234512345[root@web ~]# echo -n 12345 | tee - -
   123451234512345[root@web
~]# echo -n 12345 | tee - - -
   12345123451234512345[root@web ~]# echo -n
12345 | tee - - - -
   1234512345123451234512345[root@web ~]#
   

   示例三 使用tee命令把标准错误输出也保存到文件
   [root@web ~]# ls "*"

   ls: *: 没有那个文件或目录
   [root@web ~]# ls "*" | tee -
   ls: *:
没有那个文件或目录
   [root@web ~]# ls "*" | tee ls.txt
   ls: *: 没有那个文件或目录
  
[root@web ~]# cat ls.txt
   [root@web ~]# ls "*" 2>&1 | tee ls.txt

   ls: *: 没有那个文件或目录
   [root@web ~]# cat ls.txt
   ls: *:
没有那个文件或目录
   示例四 列出文本内容,同时复制3份复本
  
列出文本文件slayers.story的内容,同时复制3份副本,文件名称分别为ss-copy1、ss-copy2、ss-copy3:
  
[root@web ~]#  cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

Linux Shell脚本入门:tee命令的更多相关文章

  1. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  2. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  3. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  4. Linux shell 脚本入门教程+实例

    原文:http://www.wiquan.com/article/136 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是shell仍然是一个非常灵活的工具.She ...

  5. Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1

    linux空设备文件和重定向 输出/输入重导向 >      >>   <   <<   :>   &>   2&>   2< ...

  6. Linux Shell脚本入门--awk命令详解

    简单使用: awk :对于文件中一行行的独处来执行操作 . awk -F :'{print $1,$4}'   :使用‘:’来分割这一行,把这一行的第一第四个域打印出来 . 详细介绍: AWK命令介绍 ...

  7. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  8. Linux Shell脚本入门--Uniq命令

    uniq uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [ ...

  9. Linux Shell脚本入门--wc命令

    wc 统计文件里面有多少单词,多少行,多少字符. wc语法 [root@www ~]# wc [-lwm] 选项与参数: -l :仅列出行: -w :仅列出多少字(英文单字): -m :多少字符: 默 ...

随机推荐

  1. WPF之旅(三)- 布局之StackPanel

    说到WPF的界面布局,相信很多朋友都写过Html代码.在WPF中,大多数程序都使用类似Web的(flow)流布局.在使用流布局模型时,各种控件可以按特定的要求来排列,在窗口内容发生变化时,比如窗口大小 ...

  2. python之input(), raw_input()

    input(): 要求输入合法的python表达式, 例如字串需要加"", 四则运算会自动计算. raw_input():所有输入视作字串 >>> val=inp ...

  3. 在iOS App的图标上显示版本信息

    最近读到一篇文章(http://www.merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/)介绍了一种非 ...

  4. Wireshark 入门

    1.过滤目的地是百度的IP包. 百度的ip: 命令:ip.src eq 61.135.169.125 过滤ip来源是61.135.169.125 ip.dst eq 61.135.169.125 过滤 ...

  5. 浅析JVM内存结构和6大区域(转)

    其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...

  6. C# 该行已经属于另一个表 的解决方法[转]

    该文转自:http://blog.sina.com.cn/s/blog_48e4c3fe0100nzs6.html DataTable dt = new DataTable(); dt = ds.Ta ...

  7. 【HDOJ】【4089】Activation

    概率DP kuangbin总结中的第5题 题解copy: HDU 4098 题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列 ...

  8. JavaScript高级---组合模式设计

    一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...

  9. VisionTimer BUG && Start

    void Start() { vp_Timer.In(0.0f, delegate() { Debug.Log("Start"); }, 10, 1.0f); } Version ...

  10. 使用mysql触发器脚本,解决流水数据的添加。

    1.建立表脚本 CREATE DATABASE `spring` DEFAULT CHARACTER SET utf8; USE `spring`; CREATE TABLE `account` ( ...