Linux中記錄終端(Terminal)輸出到文本文件(轉載)

一,如何把命令運行的結果保存到文件當中?
  這個問題太簡單了,你們都知道,用 > 把輸出轉向就能夠了
  例子:
  [lhd@hongdi ~]$ ls > ls.txt
  [lhd@hongdi ~]$ cat ls.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
  說明: > 是把輸出轉向到指定的文件,如文件已存在的話也會從新寫入,文件原內容不會保留
       >> 是把輸出附向到文件的後面,文件原內容會保留下來

二,如何能在輸出信息的同時把信息記錄到文件中?
  咱們在上面的例子中能夠看到,咱們使用輸出轉向,命令在終端上的輸出轉向到了文件中,但若是我但願能同時在終端上看到輸出信息怎麼辦?
  咱們能夠使用這個命令: tee
  解釋一下tee的做用:
  read from standard input and write to standard output and files
  它從標準輸入讀取內容並將其寫到標準輸出和文件中
  看例子:
  [lhd@hongdi ~]$ ls | tee ls_tee.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
  [lhd@hongdi ~]$ cat ls_tee.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
備註:使用 tee時,若是想保留目標文件原有的內容怎麼辦?
  能夠使用 -a參數
  -a, --append
  append to the given FILEs, do not overwrite
  附加至給出的文件,而不是覆蓋它

三,多個命令的輸出都須要記錄,能夠用script
  script這個命令很強大,能夠記錄終端的全部輸出到相應的文件中
  看例子:
  [lhd@hongdi ~]$ script
  Script. started, file is typescript
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [lhd@hongdi ~]$ exit
  exit
  Script. done, file is typescript
  [lhd@hongdi ~]$ cat typescript
  Script. started on 2009年02月08日 星期日 18時56分52秒
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [lhd@hongdi ~]$ exit
  exit
  Script. done on 2009年02月08日 星期日 18時57分00秒
  說明:
  1,咱們在啓動script時沒有指定文件名,它會自動記錄到當前目錄下一個名爲 typescript的文件中。也能夠用 -a參數 指定文件名
  例子:
  [lhd@hongdi ~]$ script. -a example.txt
  Script. started, file is example.txt
  此時終端的輸出內容被記錄到 example.txt這個文件中
  2,退出script時,用exit
  感到奇怪嗎?事實上script就是啓動了一個shell
  看一下ps auxfww 的信息就知道了
  lhd 17738 0.1 3.2 152028 33328 ? Sl 18:30 0:03 /usr/bin/konsole
  lhd 17740 0.0 0.1 6372 1720 pts/1 Ss 18:30 0:00 \_ /bin/bash
  lhd 17900 0.0 0.0 5344 628 pts/1 S 19:01 0:00 | \_ script
  lhd 17901 0.0 0.0 5348 464 pts/1 S 19:01 0:00 | \_ script
  lhd 17902 0.5 0.1 6372 1688 pts/2 Ss 19:01 0:00 | \_ bash -i
  3,查看typescript的內容,能夠看到它同時記錄下了script的啓動和結束時間

  四,用script錄製並播放session的內容
  咱們能夠用 script把整個終端會話的全部操做和輸出錄製下來,而後再用scriptreplay進行播放。
  若是錄製時記錄下來了操做時的時間數據,那麼播放時和操做時的使用時間徹底相同。
  這個頗有用吧,好比:咱們能夠把安裝軟件時編譯的過程記錄下來,而後給別人進行演示
  看例子:
  [lhd@hongdi ~]$ script. -t 2>example.time -a example.txt
  Script. started, file is example.txt
  [lhd@hongdi ~]$ 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
  以後複製文件到系統的命令目錄中便可
  [root@hongdi 下載]# cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay
  [root@hongdi 下載]# chmod 755 /usr/bin/scriptreplay
  備註: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已無需另行安裝
  第二步:播放所錄製的session內容
  [lhd@hongdi ~]$ scriptreplay example1.time example1.txt
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar jeffray_lee@hotmail.com pass
  [lhd@hongdi ~]$ abcd
  bash: abcd: command not found

php

  [lhd@hongdi ~]$ exithtml



http://blog.sciencenet.cn/blog-758166-620475.html linux

相關文章
相關標籤/搜索