0

I want to write a bash script in linux that reveals the download speed of my internet at a certain date time + day followed by the downloading speed.

The information must be stored into a file on the same line, I want a second script to reveal the speed in a sort of "graphic" like seen below:

24/11/2016 12:01 ********** 5.2MB/s 
24/11/2016 12:03 *******    3.5MB/s 
24/11/2016 12:05 ********   3.9MB/s

i'm using speedtest-cli -simple, this command reveals my download and upload speed.

Now this is my first script :

 while true; do 
     date “´%x %X” >> Test.txt 
     speedtest-cli –simple >Speedtest.txt 
     sed '2!d' speedtest.txt >> Test.txt 
     rm Speedtest.txt 
     sleep 20 
 done

For now i want it to run until I decide to stop the script. But my problem is that the date and the download speed doesn't get stored on the same line. (also I can't figure out to just output the download speed so i use sed'2!d' to delete al exept the download line output and put it in the main file, though it wil be displayed like "download : 2.1MB/s")

The date display is correct and the graphic where problems for later but I wonder how to put the output of the commands next to each other, if i know that i will find a way to display date followed by the graphic and the download speed on the same line.

andi
  • 6,442
  • 1
  • 18
  • 43
Lewis Algawari
  • 39
  • 2
  • 10

2 Answers2

1

Do it without using any temporary files:

while sleep 20;do
   echo $(date "+%x %X") $(speedtest-cli --simple|grep ^Download) >> Test.txt
done
Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • 1
    You should wrap some things in single and double quotes: `printf '%s %s\n' "$(date "+%x %X")" "$(speedtest-cli --simple | grep '^Download')"` – Andreas Louv Dec 11 '16 at 23:12
  • Thanks! But I still have a problem. I want it to display ONLY the download speed. When I use echo $(speedtest |grep ^Download it displays : "Download: 52.2 Mb/s" Is there any way to display only the speed? Because I need this "parameter for the graphic I'm going to create in the second script – Lewis Algawari Dec 12 '16 at 15:39
  • `grep -oP '^Download: \K.*'` – Ipor Sircer Dec 12 '16 at 15:43
0

(From this question) You can simply use echo -n <text> to eliminate the linebreak at the end. For instance, do:

echo -n `date "+%x %X"` >> Test.txt 
Community
  • 1
  • 1