Adding internet check to each script

This commit is contained in:
Alex Epstein 2017-06-26 19:28:27 -04:00
parent 50083b19a7
commit fa881976af
3 changed files with 32 additions and 2 deletions

View File

@ -51,13 +51,25 @@ getAmount()
fi
}
checkInternet()
{
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
echo "Error: no active internet connection" >&2
return 1
fi
}
convertCurrency()
{
exchangeRate=$(curl -s "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9][.][0-9]*") > /dev/null
exchangeAmount=$(echo "$exchangeRate * $amount" | bc )
echo "$amount $base is equal to $exchangeAmount $exchangeTo"
}
checkInternet || exit 1
getBase
getExchangeTo
getAmount

View File

@ -7,7 +7,7 @@ checkInternet()
if [ $? -eq 0 ]; then
return 0
else
echo "Error no active internet connection" >&2
echo "Error: no active internet connection" >&2
return 1
fi
}

View File

@ -1,5 +1,7 @@
#!/bin/sh
# Author: Alexander Epstein https://github.com/alexanderepstein
getWeather()
{
country=$(curl -s ipinfo.io/country) > /dev/null
if [ $country = "US" ];then
city=$(curl -s ipinfo.io/city) > /dev/null
@ -10,3 +12,19 @@ else
location=$(curl -s ipinfo.io/loc) > /dev/null
curl wttr.in/$location
fi
}
checkInternet()
{
echo "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
echo "Error: no active internet connection" >&2
return 1
fi
}
checkInternet || exit 1
getWeather