Sunday, August 18, 2013

Conditional Statement in Bash on OS X

I had a bit of trouble getting a conditional statement to evaluate on my bash script in OS X.
I was trying to perform an action if I was online or not.

A good way to test if you are online - ping google of course!

For testing purposes I just got the computer to use text to speech to let me know the result.
Here is my working example, spaces are important!



ping -q -c 1 && FLAG=1
if [ $FLAG = "1" ] ; then
say $FLAG
say $FLAG


If you can ping google,the computer will speak "one"

If you you want to test, change to something you cant resolve such as and run it again.

You shouldn't be able to ping so the computer will speak "zero"

Of course this is a simple example and you need to tailor to your situation, just because you can't ping google doesn't mean you are not online. For example pings could be blocked on your network, however this suits my situation.


No comments:

Post a Comment