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

來源:cssiqi的專欄

http://www.2cto.com/os/201307/224002.html


 

 一,如何把命令運行的結果保存到文件當中?

  這個問題太簡單了,大家都知道,用 > 把輸出轉向就可以了

  例子:

  [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 [email protected] pass

  [lhd@hongdi ~]$ abcd

  bash: abcd: command not found

 

  [lhd@hongdi ~]$ exit


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章