【问题标题】:bash can't capture output from aria2c to variable and stdoutbash 无法将 aria2c 的输出捕获到变量和标准输出
【发布时间】: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 脚本执行此操作吗?

【问题讨论】:

标签: bash variables stdout capture aria2


【解决方案1】:

由于 aria2c 似乎输出到 stdout,考虑 teeing 到 stderr:

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

stdout 以 var 结尾,而 tee 将其复制到 stderr,后者会显示在您的屏幕上。

【讨论】:

  • 不幸的是,这对我不起作用。我看到的行为与我的示例相同 - 变量赋值捕获输出,因此在下载完成之前没有任何可用的内容。
  • 试试 --log log.txt 和 tail log.txt ?
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2022-07-29
  • 2021-02-08
相关资源
最近更新 更多