ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Linux 输出或保存终端显示

Linux 输出或保存终端显示

原创 Linux操作系统 作者:hky87 时间:2012-09-28 17:21:16 0 删除 编辑
如何在输出信息的同时将其保存在文件中?
有三种方式
1. 直接将命令的结果用 > 把输出转向
[root@www ~]# ls >ls.txt
[root@www ~]# cat ls.txt
anaconda-ks.cfg
cronolog-1.6.2
install.log
install.log.syslog
ls_tee.txt
ls.txt
typescript

2.利用tee在输出信息的同时保存到文件中
tee的含义:read from standard input and write to standard output and files

[root@www ~]# ls | tee ls_tee.txt
anaconda-ks.cfg
cronolog-1.6.2
install.log
install.log.syslog
ls_tee.txt
ls.txt
typescript
[root@www ~]# cat ls_tee.txt
anaconda-ks.cfg
cronolog-1.6.2
install.log
install.log.syslog
ls_tee.txt
ls.txt
typescript

3.多个命令的输出都需要记录,可以用script


[root@www ~]# script
Script. started, file is typescript
[root@www ~]# ls
anaconda-ks.cfg  install.log         ls_tee.txt  typescript
cronolog-1.6.2   install.log.syslog  ls.txt
[root@www ~]# exit
exit
Script. done, file is typescript
[root@www ~]# cat typescript
Script. started on Fri 28 Sep 2012 05:27:02 PM CST
[root@www ~]# ls
anaconda-ks.cfg  install.log         ls_tee.txt  typescript
cronolog-1.6.2   install.log.syslog  ls.txt
[root@www ~]# exit
exit

Script. done on Fri 28 Sep 2012 05:27:12 PM CST

在启动script时没有指定文件名,它会自动记录到当前文件夹下一个文件名为typescript的文件中。也可以用-a 参数  指定文件名
[root@www ~]# script. -a example.txt

另外还可以录制

并播放session的内容

  我们可以用 script把整个终端会话的所有操作和输出录制下来,然后再用scriptreplay进行播放。

  如果录制时记录下来了操作时的时间数据,那么播放时和操作时的使用时间完全相同,

  这个很有用吧,比如:我们可以把安装软件时编译的过程记录下来,然后给别人进行演示

  看例子:

  $ script. -t 2>example.time -a example.txt

  Script. started, file is example.txt

  $ ls

  说明: -t 2>example.time -t是把时间数据输出到标准错误(standard error)

  所以我们使用 2>example.time 把数据转向到 example.time这个文件当中

  如何播放所记录的内容?

  第一步:安装scriptreplay

  下载

  wget linux/utils/util-linux/util-linux-2.12r.tar.bz2">ftp://ftp.kernel.org/pub/linux/utils/util-linux/util-linux-2.12r.tar.bz2

  解压

  tar -jxvf util-linux-2.12r.tar.bz2

  之后复制文件到系统的命令目录中即可

  # cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay

  # chmod 755 /usr/bin/scriptreplay

  备注: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已无需另行安装

  第二步:播放所录制的session内容

  $ scriptreplay example1.time example1.txt

  $ ls

  1.gtkrc-2.0 c.tar pass

  $ abcd

  bash: abcd: command not found

  $ exit


录制及播放没有测试过

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10314474/viewspace-745392/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Linux文件目录
请登录后发表评论 登录
全部评论

注册时间:2009-10-09

  • 博文量
    87
  • 访问量
    345409