There are multiple ways to check whether or not you got internet connection. The most reliable way that I found is to use the following code.
# Check for internet connection. if wget -q --spider "http://google.com"; then echo "Success, got internet connection!" else echo "Error: No internet connection!" fi
Output
Success, got internet connection!