博客專欄

        EEPW首頁 > 博客 > Linux中記錄終端(Terminal)輸出到文本文件

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

        發布人:電子禪石 時間:2021-04-22 來源:工程師 發布文章

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

          這個問題太簡單了,大家都知道,用 > 把輸出轉向就可以了
          例子:
          [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

          [lhd@hongdi ~]$ exit


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞: Terminal

        技術專區

        關閉
        主站蜘蛛池模板: 满城县| 钟山县| 长汀县| 洪湖市| 石棉县| 互助| 桃园县| 宝丰县| 昌平区| 大田县| 枣庄市| 清新县| 年辖:市辖区| 会昌县| 博爱县| 远安县| 宜阳县| 清丰县| 杭州市| 景宁| 海南省| 红安县| 常州市| 定边县| 临清市| 磐安县| 思南县| 康平县| 成都市| 六安市| 泸西县| 汉寿县| 泾源县| 镇江市| 永城市| 龙州县| 巫溪县| 亚东县| 三江| 离岛区| 深水埗区|