I'm new to bash and I'm stuck at trying to negate the following command:
wget -q --tries=10 --timeout=20 --spider http://google.com if [[ $? -eq 0 ]]; then echo "Sorry you are Offline" exit 1
This if condition returns true if I'm connected to the internet. I want it to happen the other way around but putting !
anywhere doesn't seem to work.
How to negate an if condition in a Bash if statement? (if not command or if not equal) To negate any condition, use the ! operator, for example: if ! <test-command>; then <command-on-failure>; fi .
In Scala, you can check if two operands are equal ( == ) or not ( != ) and it returns true if the condition is met, false if not ( else ). By itself, ! is called the Logical NOT Operator. Use it to reverse the logical state of its operand.
The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
You can choose:
if [[ $? -ne 0 ]]; then # -ne: not equal if ! [[ $? -eq 0 ]]; then # -eq: equal if [[ ! $? -eq 0 ]]; then
!
inverts the return of the following expression, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With