Files
如何將終端顯示的輸出保存到 txt 文件?
我已經嘗試過,
script
但它不起作用。milenko@milenko-desktop:~/MTM$ script output.txt Script started, file is output.txt milenko@milenko-desktop:~/MTM$ wc -l output.txt 0 output.txt
由於執行 C 程式碼,我的螢幕上有超過 4000000 行。只是其中的一部分
4194282 0.166666 2.35285e-06 -56.2841 -137.299 -129.386 4 0.974001 4194283 0.166666 2.2536e-06 -56.4712 -129.67 -131.872 4 0.577388 4194284 0.166666 2.33943e-06 -56.3089 -131.649 -132.051 4 0.693404 4194285 0.166666 2.40782e-06 -56.1838 -139.678 -129.377 4 0.702479 4194286 0.166666 2.61584e-06 -55.8239 -125.733 -128.783 4 0.585886 4194287 0.166666 2.60877e-06 -55.8356 -129.239 -127.815 4 0.773082 4194288 0.166666 2.71129e-06 -55.6682 -129.591 -127.553 4 0.796556 4194289 0.166666 2.89225e-06 -55.3876 -125.257 -127.627 4 0.815006 4194290 0.166666 2.84658e-06 -55.4568 -129.576 -127.566 4 0.557463 4194291 0.166666 2.71516e-06 -55.662 -129.31 -128.904 4 0.797976 4194292 0.166666 2.56104e-06 -55.9158 -127.996 -128.422 4 0.895766 4194293 0.166666 2.6007e-06 -55.8491 -128.079 -129.663 4 0.503785 4194294 0.166666 2.40808e-06 -56.1833 -140.025 -128.88 4 0.857429 4194295 0.166666 2.33237e-06 -56.322 -126.524 -130.575 4 0.410364 4194296 0.166666 2.46136e-06 -56.0883 -135.197 -130.224 4 0.833398 4194297 0.166666 2.48021e-06 -56.0551 -134.945 -131.935 4 0.856674 4194298 0.166666 2.43826e-06 -56.1292 -128.865 -128.875 4 0.490521 4194299 0.166666 2.45184e-06 -56.1051 -126.444 -129.16 4 0.935146 4194300 0.166667 2.51457e-06 -55.9954 -141.302 -130.337 4 0.745215 4194301 0.166667 2.47265e-06 -56.0684 -133.622 -131.74 4 0.541706 4194302 0.166667 2.45156e-06 -56.1056 -128.236 -128.832 4 0.503959 4194303 0.166667 2.54703e-06 -55.9397 -127.012 -127.581 4 1.10325 4194304 0.166667 2.55029e-06 -55.9341 -132.414 -127.012 4 0.475155
如何保存?
如果我嘗試
script > output.txt
Script started, file is typescript ]0;milenko@milenko-desktop: ~/MTM[01;32mmilenko@milenko-desktop[00m:[01;34m~/MTM[00m$ exit Script done, file is typescript
我也試過
command | tee ~/outputfile.txt
但又一次
wc -l outputfile.txt 0 outputfile.txt
所以這不是正確的方法。
當我需要擷取 stdout 和 stderr 時,我的小秘密是使用nohup。當您只對輸出感興趣而不是螢幕上的互動時,這是一個很好的解決方案。
擷取標準輸出(僅)並將其顯示在螢幕上的經典方法是使用命令tee
nohup script.sh | tee merged.out
如果只需要標準輸出
script.sh | tee script.out
這是一個使用命令
ls
而不是 script.sh的快速範例xx069:~ # nohup ls | tee merged.out nohup: ignoring input and redirecting stderr to stdout bin inst-sys merged.out xx069:~ # cat merged.out bin inst-sys merged.out
當然,也可以使用 2>&1 等重定向來組合 stdout 和 stderr。
主要是
script
用來捕捉螢幕上的輸入和輸出。當只需要輸出時,我根據需要使用>
、>>
、2>&1 >
、tee
和tee -a
- 之一。而nohup
當我懶惰的時候。
如果您使用
script
命令:script history_log.txt
然後做任何你想做的事,當你完成輸入:
exit
如果您的意思是腳本的輸出,則重定向標準輸出:
script.sh > output.txt
>
將標準輸出重定向到您想要的任何商品2>
重定向標準錯誤&>
重定向標準輸出和標準錯誤