[英]bash can't capture output from aria2c to variable and stdout
我正在尝试使用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脚本中做到这一点吗?
由于aria2c似乎输出到标准输出,考虑tee
荷兰国际集团,为标准错误:
var=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath | tee /dev/fd/2)
stdout以var
结尾,而tee
将其复制到stderr,stderr将显示在屏幕上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.