Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negate if condition in bash script

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.

like image 257
Sudh33ra Avatar asked Oct 20 '14 21:10

Sudh33ra


People also ask

How do you negate if in bash?

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 .

How do you negate an IF condition?

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.

How do you end an if statement in shell script?

The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.

What does [- Z $1 mean in bash?

$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.


1 Answers

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.

like image 84
Cyrus Avatar answered Sep 25 '22 23:09

Cyrus