
#!/bin/bash -x
limiter=0;
while [ $limiter -lt 10 ];do
chek=$(ping google.com -c 1 | awk '/data/ {print $7}')
if [ "$chek" = "data." ]; then
play /home/your-pc/hack/snd/Alarm.wav
exit 0
else
play /home/your-pc/hack/snd/gitar.wav
let limiter+=1
fi
done
This simple bash script was made to check your current connectivity. It ping google.com and play sounds until it meet a few conditions to stop.
- First condition is when the stringย $chek contains “data.” , I’ll explain it later.
- Second condition is when the $limiter reach 9
First, we assign 0 to a variable called limiter .Then..
while [ $limiter -lt 10]; do
In this line, we set a loop using while..do with a condition. the “-lt” means lesser than or < . So when the $limiter value is 10, it then stop the looping.
chek=$(ping google.com -c 1 | awk '/data/ {print $7}')
This is how you assign a variable’s value from output of commands. ping google.com -c 1 will do a ping to google.com one time. The output of ping will be used in awk ‘/data/ {print $7}‘ย AWK is a programming language that is designed for processing text-based data.
For example: if the connectivity is available the ping output will be.
PING google.com (216.239.61.104) 56(84) bytes of data.
and if the connectivity is unavailable, in some relatively-normal-case it just show this.
PING: unknown host google.com
The awk line is awk ‘/data/ {print $7}’. It means it will find the line containing word “data”, and then print the 7th column of the line. If connectivity is available the seventh line is “data.” (notes: the first column is “PING”).ย And that is why in the next line of the script it will do comparison to check wether the string $chek is “data.”. This means, if connectivity is unavalaible the ping output would be as shown above then $chek does not contain “data.” or empty ” “.
if [ "$chek" = "data." ]; then
play /home/your-pc/hack/snd/Alarm.wav
exit 0
else
play /home/your-pc/hack/snd/gitar.wav
let limiter+=1
fi
done
We are ready to check wether we should hear Alarm sounds or the gitar sounds. ๐
I used the play tool to play the sounds. My friend said that it only a python script that runs some other scripts or library to play sounds. Most linux distributions has it pre-installed (He guess). So if you use ubuntu 9.10 as same as I use, this will be okay. Let’s proceed! (wait, what? windows? go search for batch tutorial)
As you can see, in line 01 the script does the comparison wether the $chek is contain “data.” or empty ” “.ย If it is contain “data.” then it will start the play commands, and start to play the sounds found in /home/your-pc/hack/snd/Alarm.wav .ย After it done play the sound, then it will simply stop the loop and finish the script.
But, when $chek does not contain “data.” or empty ” ” this means you have no connectivity, and it will play the sounds found in /home/your-pc/hack/snd/gitar.wav .It’s not over yet, after done play the sounds,
in line 06 it will increment the $limiter values to 1–>
then finish the ifย –> check wether the $limiter is now less than 10 (wich means 9)–> oh no it is not yet 9! –>
do the ping again –> assign the new values for $chek –> do the rest code all over again. –>
UNTIL, the $limiter finally reach nine and then finish the loop and stop the script. Why? Because, maybe in some case it is not about losing connectivity temporary. Without the while looping the script will play the sounds until it finally connected (wich we might sure,that it would be for a long-long time). And that can be iritating, but yes you can do the Ctrl-C to terminate the script if you run this directly from the terminal.
(what? you run this script from terminal?? I don’t want to see those scary blinking cursor with black and white text around it!)
okay, I’ll documenting once again how you can run this script simply from your top panel bar and run it with just a click. ๐
So you can do other things more useful than hitting reload button all over again :p, while listening to smooth sound of looping guitar pattern.
Oh! This, maybe you interested to another bash script implementation for the good use of linux. My other friend share it here.
AND IF YOU THINK YOU ARE HARDCORE PROGRAMMER, WICH APPARENTLY LAUGHING AT MY SCRIPT>>> Please..pleasee..pleaseeee… give me another algorithm that probably more simple than my stupid script…please…