Welcome to the Bash Challenge #9 by Yes I Know IT & It’s FOSS. In this weekly challenge, we will show you a terminal screen, and we will count on you to help us obtaining the result we wanted. There can be many solutions, and being creative is the most amusing part of the challenge.
If you haven’t done it already, do take a look at previous challenges:
Ready to play? So here is this week’s challenge:
My command outputs are in the wrong order!
This week, I want a shell function to log the round trip time (rtt) to a server. Only if the ping command has succeeded, I want to record the date of the measure on the line below the rtt. Given those requirements, I end up with that solution:
probe() ( ping -qnc2 www.google.com | \ grep rtt & \ date +"OK %D %T" ) rm -f log probe >> log probe >> log cat log
But, for some reason, the date and rtt lines are swapped in the log file?!? Why? Could you fix that? Is there a cleaner way to achieve my goal?
Answering those questions is your challenge.
To create this challenge, I used:
- GNU Bash, version 4.4.5 (x86_64-pc-linux-gnu)
- Debian 4.8.7-1 (amd64)
- All commands are those shipped with a standard Debian distribution
- No commands were aliased
We’ll be publishing the solution next week. Meanwhile, don’t forget to exercise your brain to figure out the solution here and mention your approach in the comment section below.