linux - 输出自动更新到屏幕和文件的文本

标签 linux bash scripting

我正在尝试使用一个脚本来为我更新我的系统,并将其文本输出到文件和屏幕。使用 tee 这相当简单,除了我的更新管理器 pacman 输出与 wget 相同类型的文本。这是一些示例输出。

 core                                                                              107.0 KiB   392K/s 00:00 [################################################################] 100%
 extra                                                                            1531.8 KiB   719K/s 00:02 [################################################################] 100%
 community                                                                           2.1 MiB   818K/s 00:03 [################################################################] 100%

当我尝试使用 tee 将此类输出重定向到文件时,这是我在屏幕和文件中得到的输出。

:: Synchronizing package databases...
downloading core.db...
downloading extra.db...
downloading community.db...

我明白这是因为 pacman 使用缓冲区输出到屏幕,但我仍然希望有一种方法可以将状态输出到屏幕和文件,而不仅仅是“正在下载 xyz...”。

预先感谢您的帮助。

编辑:

我当前没有更新,但这里有一些示例输出,与我安装 Opera 浏览器非常相似。带有百分比和井号的行是随下载进度更新的缓冲区。

resolving dependencies...
looking for inter-conflicts...

Packages (1): opera-12.16.1860-2

Total Download Size:    10.49 MiB
Total Installed Size:   45.03 MiB

:: Proceed with installation? [Y/n] y
:: Retrieving packages ...
 opera-12.16.1860-2-x86_64                                                          13.4 MiB  1151K/s 00:12 [################################################################] 100%
(1/1) checking keys in keyring                                                                              [################################################################] 100%
(1/1) checking package integrity                                                                            [################################################################] 100%
(1/1) loading package files                                                                                 [################################################################] 100%
(1/1) checking for file conflicts                                                                           [################################################################] 100%
(1/1) checking available disk space                                                                         [################################################################] 100%
(1/1) installing opera                                                                                      [################################################################] 100%
Optional dependencies for opera
    gstreamer0.10-base-plugins: HTML5 Video support
    gstreamer0.10-good: HTML5 Video support

这就是上面的输出通过 tee 重定向时的样子。

warning: opera-12.16.1860-2 is up to date -- reinstalling
resolving dependencies...
looking for inter-conflicts...

Packages (1): opera-12.16.1860-2

Total Installed Size:   45.03 MiB
Net Upgrade Size:       0.00 MiB

:: Proceed with installation? [Y/n] y
checking keyring...
checking package integrity...
loading package files...
checking for file conflicts...
checking available disk space...
reinstalling opera...

如您所见,未显示下载进度。只有三个时期。

最佳答案

请注意,如果我完全理解这个问题,但我认为您的脚本可能会输出到 stdout 和 stderr,并且您希望将两者捕获到文件并显示在终端上。在这种情况下,您可以通过将脚本的 stderr 重定向到 stderr 来完成此操作,然后将整个内容传输到 tee:

./myscript.sh 2>&1 | tee output.log

所以我认为您还想要所有有关进度条等的输出。您可以使用script命令来获取:

script -e -q -c "./myscript.sh" output.log 

这对我来说适用于 wget:

终端输出:

ubuntu@ubuntu:~$ script -e -q -c "wget http://stackoverflow.com" output.log
--2014-02-21 20:01:31--  http://stackoverflow.com/
Resolving stackoverflow.com (stackoverflow.com)... 198.252.206.140
Connecting to stackoverflow.com (stackoverflow.com)|198.252.206.140|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 214108 (209K) [text/html]
Saving to: `index.html.5'

100%[======================================>] 214,108     7.59K/s   in 38s     

Last-modified header invalid -- time-stamp ignored.
2014-02-21 20:02:13 (5.55 KB/s) - `index.html.5' saved [214108/214108]

ubuntu@ubuntu:~$

输出.log的内容:

Script started on Fri 21 Feb 2014 08:01:31 PM PST
--2014-02-21 20:01:31--  http://stackoverflow.com/
Resolving stackoverflow.com (stackoverflow.com)... 198.252.206.140
Connecting to stackoverflow.com (stackoverflow.com)|198.252.206.140|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 214108 (209K) [text/html]
Saving to: `index.html.5'


 0% [                                       ] 0           --.-K/s              
 1% [                                       ] 3,367       3.66K/s              
 2% [>                                      ] 5,863       5.09K/s              
 3% [>                                      ] 7,111       3.54K/s              
 5% [>                                      ] 10,855      3.73K/s              

### Output omitted - I have a slow connection right now ###

97% [=====================================> ] 209,806     7.84K/s  eta 1s      
99% [=====================================> ] 212,302     7.44K/s  eta 1s      
100%[======================================>] 214,108     7.59K/s   in 38s     

Last-modified header invalid -- time-stamp ignored.
2014-02-21 20:02:13 (5.55 KB/s) - `index.html.5' saved [214108/214108]

关于linux - 输出自动更新到屏幕和文件的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21949040/

相关文章:

linux - bash变量似乎无法分配给在容器中执行的psql输出

javascript - 如何从网站执行批处理文件

linux - 使用 -C(或 -B/-A)运行时在 grep 结果之间输出空行

linux - 如何修复尝试调试简单的 nasm 汇编程序时出现 "sigsegv"的问题

linux - Hbase shell 远程命令

scripting - Adobe Premiere 脚本

bash - 如何检查 bash 脚本中文件名的扩展名?

c++ - CMake 文件找不到 Qt 图表模块

linux - 如何创建没有外部依赖的wxWidgets应用程序?

bash - 如何终止从特定目录运行的所有进程?