2

I am trying to use aria2c to download a file. 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.

I have tried using different combinations of 2>&1, and | tee /dev/tty at the end of the command, but nothing shows in the display in realtime.

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. Anyone had any luck doing this from a bash script?

user1199956
  • 109
  • 1
  • 10
  • Seems like a buffering problem. Check this post: [Force line-buffering of stdout when piping to tee](https://stackoverflow.com/questions/11337041/force-line-buffering-of-stdout-when-piping-to-tee). – codeforester Aug 02 '17 at 23:51

1 Answers1

1

Since aria2c seems to output to stdout, consider teeing that to stderr:

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.

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
  • Unfortunately, this didn't work for me. I see the same behavior as my examples - the variable assignment captures the output so nothing's available until the download completes. – user1199956 Aug 15 '17 at 21:00
  • try --log log.txt and tail log.txt ? – caoanan Apr 03 '19 at 02:37