Reputation: 2633
I am trying to save the outputs of fping in to a variable in Bash, this should be easy but I just cant get it to work. I have tried various methods, tried using things like AWK and CUT on the captured variable but comming up with empty varibles.
My thought process is as follows.
8.8.8.8 : [0], 84 bytes, 15.1 ms (15.1 avg, 0% loss) 8.8.8.8 : [1], 84 bytes, 15.0 ms (15.0 avg, 0% loss) 8.8.8.8 : xmt/rcv/%loss = 2/2/0%, min/avg/max = 15.0/15.0/15.1
8.8.8.8 : xmt/rcv/%loss = 2/2/0%, min/avg/max = 15.0/15.0/15.1
and I expected to save that last line in to a variable that I can then further process. But instead i get a BLANK variable even though it shows the line as below??
$ output=$(fping -c 1 8.8.8.8 | awk '/min/')
8.8.8.8 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 15.1/15.1/15.1
I was also looking at first using AWK to extract jsut the 5 and 6th colum values to make post processing easier
something like
output=$(fping -c 1 8.8.8.8 | awk '/min/ {loss= $5, time=$6}')
this syntax may be wrong at the moment but to give a variable like bwlow with all the values ready to extract
"2/2/0% 15.0/15.0/15.1"
What am i doing wrong? how can i save that last line of the output in to a variable? I am ok splitting it up, but why does the AWK not extract the right bit and save it?
Thank you
Upvotes: 5
Views: 1154
Reputation: 123470
Here's the complete, unabbreviated output of your attempt:
user@host$ output=$(fping -c 1 8.8.8.8 | awk '/min/')
8.8.8.8 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.66/0.66/0.66
user@host$
The fact that you're getting output on screen is crucial, it means the data is not being captured. That typically indicates the data is written to stderr instead. Here's what you get when you redirect stdout to stderr:
user@host$ output=$(fping -c 1 8.8.8.8 2>&1 | awk '/min/')
(no output)
and indeed, the variable now has a value:
user@host$ printf '%s\n' "$output"
8.8.8.8 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.77/0.77/0.77
Upvotes: 5