So I'm pretty new to Linux and I really can't figure it how to.
So I wanted to make a useful output for our monitoring Tool with speedtest-cli
. We have to monitor download and upload speeds for multiple locations.
I made following script that breaks the output with awk and gives me the desired number (in this case only the number itself without text in front and behind the number)
SP=$(speedtest-cli 2>&1)
if [ $? -eq 0 ]
then
Down=$(echo $SP | gawk '{split($0,a,":"); print a[3]}' | \
gawk '{split($0,a," "); print a[1]}')
fi
echo "$Down"
This script works as I want it to. But, I really would like a solution to return only the digits. So is it possible to search for the line "Download: 90.00 Mbit/s" and take the 90.00 and give that to output?
EDIT:
for anyone interested the script I wrote down there. it outputs <WAN_IP>,<Download>,<Upload>
and if there is no connection it outputs 0.0.0.0,0,0
#!/bin/sh
SP=$(speedtest-cli 2>&1)
if [ $? -eq 0 ]
then
From=$(echo "$SP" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
Down=$(echo "$SP" | gawk '{if (match($0,/Download: ([[:digit:]]+\.[[:digit:]]+) Mbit\/s/,a)>0) print a[1]}')
Up=$(echo "$SP" | gawk '{if (match ($0,/Upload: ([[:digit:]]+\.[[:digit:]]+) Mbit\/s/,a)>0) print a[1]}')
else
From="0.0.0.0"
Down="0"
Up="0"
fi
echo "$From,$Down,$Up"
So you can add this as last iteration.
– realpclaudio Feb 13 '20 at 13:20speedtest
call so it is easier to help you design a suitable regular expression. – AdminBee Feb 13 '20 at 13:30Download
immediately, or is there some indentation with leading whitespace? Does the line end immediately after theMbit/s
, or are there trailing characters? Are there further lines in the output that might start withDownload
or contain similar patterns? I think your problem can be solved, but that kind of information would be necessary to provide good advice. – AdminBee Feb 13 '20 at 13:50Download: 90.00 Mbit/s
is one line. In the variable it just in the middle of the long string with every output line – Marinus Pollinger Feb 13 '20 at 13:55"$SP"
(which you should do anyway in most cases). – AdminBee Feb 13 '20 at 14:05