0

When mbuffer finishes on the receiving side it prints the time and speed, which I would like to get into $time and $speed.

Trying to implement this answer

#!/bin/bash
exec 3>&1 4>&2 #set up extra file descriptors
error=$( { mbuffer -v 0 -4 -s 128k -m 1G -I 8023 3>&1 4>&2 > /tank3/fs5/tst; } )
exec 3>&- 4>&- # release the extra file descriptors
echo "The message is \"${error}\""

Executing this and in another terminal

echo secret | mbuffer -4 -s 128k -m 1G -O localhost:8023

I get

# ./fff 

summary:  0.0 KiByte in  0.1 sec - average of  0.0 KiB/s
The message is ""
secret

where I was hoping to see the summary message between the "".

Question

I assume that the summary message must be printed to STDERR, as I pipe STDOUT to a file, and its content is correct.

Can anyone see what I am doing wrong?

Community
  • 1
  • 1
Sandra Schlichting
  • 24,425
  • 31
  • 103
  • 150

2 Answers2

1

try this:

#!/bin/bash
exec 3>&1
error=$({ mbuffer options >&3 ; } 2>&1)
exec 3>&-
echo "The message is \"${error}\"" >&2

This will pipe stdin through mbuffer to stdout, and write the processed stderr to stderr.

mata
  • 63,859
  • 10
  • 155
  • 155
1

I think you may have tried to apply the wrong solution. The one you referenced dealt with a somewhat more complicated problem. If your goal is to direct stdout to a file and capture stderr, this should do it (yes, it's counterintuitive to map stderr to stdout THEN stdout to a file, but that's how redirection works, you're sending stream 2 to where stream 1 points to at that moment, which is stdout)

error=$(mbuffer -v 0 -4 -s 128k -m 1G -I 8023 2>&1 > /tank3/fs5/tst)
echo "The message is \"${error}\""
William
  • 4,507
  • 2
  • 13
  • 14