Negate if condition in bash script Negate if condition in bash script bash bash

Negate if condition in bash script


You can choose:

if [[ $? -ne 0 ]]; then       # -ne: not equalif ! [[ $? -eq 0 ]]; then     # -eq: equalif [[ ! $? -eq 0 ]]; then

! inverts the return of the following expression, respectively.


Better

if ! wget -q --spider --tries=10 --timeout=20 google.comthen  echo 'Sorry you are Offline'  exit 1fi


If you're feeling lazy, here's a terse method of handling conditions using || (or) and && (and) after the operation:

wget -q --tries=10 --timeout=20 --spider http://google.com || \{ echo "Sorry you are Offline" && exit 1; }