简体   繁体   English

bash无法从aria2c捕获到变量和stdout的输出

[英]bash can't capture output from aria2c to variable and stdout

I am trying to use aria2c to download a file. 我正在尝试使用aria2c下载文件。 The command looks like this: 该命令如下所示:

aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath

The command works perfectly from the script when run this way. 以这种方式运行时,该命令在脚本中可以完美运行。 What I'm trying to do is capture the output from the command to a variable and still display it on the screen in real-time. 我想要做的是将命令的输出捕获到变量中,并仍将其实时显示在屏幕上。

I have successfully captured the output to a variable by using: 我已成功使用以下命令将输出捕获到变量中:

VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath)

With this scenario though, there's a long delay on the screen where there's no update while the download is happening. 但是,在这种情况下,屏幕上会存在很长的延迟,在进行下载的过程中没有更新。 I have an echo command after this line in the script and $VAR has all of the aria2c download data captured. 我在脚本中的这一行之后有一个echo命令,并且$ VAR具有捕获的所有aria2c下载数据。

I have tried using different combinations of 2>&1, and | 我尝试使用2>&1和|的不同组合。 tee /dev/tty at the end of the command, but nothing shows in the display in realtime. 在命令末尾使用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 )

I've been able to use the "2>&1 | tee" combination before with other commands but for some reason I can't seem to capture aria2c to both simultaneously. 我已经可以在其他命令中使用“ 2>&1 | tee”组合,但是由于某种原因,我似乎无法同时捕获aria2c和这两个命令。 Anyone had any luck doing this from a bash script? 有人从bash脚本中做到这一点吗?

Since aria2c seems to output to stdout, consider tee ing that to stderr: 由于aria2c似乎输出到标准输出,考虑tee荷兰国际集团,为标准错误:

var=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath | tee /dev/fd/2)

The stdout ends up in var while tee duplicates it to stderr, which displays to your screen. stdout以var结尾,而tee将其复制到stderr,stderr将显示在屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM