【发布时间】:2018-01-10 07:15:07
【问题描述】:
我正在尝试使用 aria2c 下载文件。该命令如下所示:
aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath
以这种方式运行时,该命令可以完美地从脚本中运行。我想要做的是将命令的输出捕获到一个变量中,并仍然实时显示在屏幕上。
我已成功使用以下方法将输出捕获到变量:
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath)
不过,在这种情况下,屏幕上会出现很长的延迟,在下载过程中没有更新。我在脚本的这一行之后有一个 echo 命令,并且 $VAR 捕获了所有 aria2c 下载数据。
我尝试过使用 2>&1 和 | 的不同组合tee /dev/tty 在命令的末尾,但没有实时显示。
Example:
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath 2>&1)
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath 2>&1 | tee /dev/tty )
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath | tee /dev/tty )
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1)
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1 | tee /dev/tty )
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1 ) | tee /dev/tty )
我之前可以将“2>&1 | tee”组合与其他命令一起使用,但由于某种原因,我似乎无法同时将 aria2c 捕获到两者。任何人都可以通过 bash 脚本执行此操作吗?
【问题讨论】:
-
似乎是缓冲问题。查看此帖子:Force line-buffering of stdout when piping to tee.
标签: bash variables stdout capture aria2