. Advertisement .
..3..
. Advertisement .
..4..
Bash If Not condition has been pretty popular in value comparisons, especially for pairs of strings and integers. In today’s article, ITtutoria will illustrate three common examples for better understanding.
Examples of Bash If Not Condition
Example 1. Compare Two String Values
In this example, we will use “if-not” to inspect which string of the two is inferior. For such tasks, we decided to add a “Bash” support command into the file, along with u2 and u1 – two string variables (u1 has greater values than u2).
Once done, we start the statement “if” with the operator “!” to see whether the u1 values lag behind u2’s.
For the condition “then” part, it’s a must to set our echo statements as “u2 is less than u1”. Why? That’s because once “<” returns “false”, “!” will reverse everything to “true”.
#!/bin/bash
u1="Hello America"
u2="Hello"
if ! [ [ "$u1" < "$u2" ]]; then
echo "u2 is LESS than u1";
else
echo "u1 is less than u2";
fi
Once the code has successfully been executed using Bash instruction, the results will undergo changes due to specified conditions and send back accurate values.
$ bash ifnot. sh
u2 is LESS than u1
Example 2. Compare Two Integer Variables
This time, we use “if-not” to inspect and compare integer variables. Before starting, we have already initialized u1 and u2 – two different integer variables – with integer values. Now, the next step is to use the statement “if” with the operator “not” to assess whether their values are comparable.
The program cannot be completed without the Bash operator “-eq” in your condition clause. If both values are comparable – and your operator “-eq” sends back “True”, then “!” will reverse everything to “False”.Hence, the “else” tail will start its execution with “EQUAL” statements from your echo arguments.
#!/bin/bash
u1=44
u2=13
if ! [[ "$u1" -eq "$u2" ]]; then
echo "NOT EQUAL";
else
echo "EQUAL";
fi
Once the Bash code runs, we receive the output “NOT EQUAL” since u1 and u2 are not the same.
~$ bash ifnot. sh
NOT EQUAL
This time, let’s alter the code by adding similar values to both variables (u1 = 13 and u2 = 13). We also upgraded the inner conditions for both of them. Now, we will be using “-ne” (the operator “not equal”) to check whether these values aren’t equal.
#!/bin/bash
u1=13
u2=13
if ! [[ "$u1" -ne "$u2"]]; then
echo "EQUAL";
else
echo "NOT EQUAL";
fi
When we run these codes, the output confirms that the results are similar as expected.
~$ bash ifnot. sh
EQUAL
Example 3. Check Whether A Variable Is Blank
Now, we will use “if-not” to inspect different conditions, accompanied by the operator “-z” to see whether our variables are empty. First of all, you should start these codes by initializing empty variables “u”. The system will send back “Empty” if your “if-not” operator sends back a “True”. Otherwise, they will return “Empty”.
#!/bin/bash
u=""
if ! [[ -z "$u" ]]; then
echo "Not Empty";
else
echo "Empty";
fi
Since the “u” variable is empty, we will receive “Empty” as the output.
~$ bash ifnot. .sh
Empty
Conclusion
Our guides have illustrated examples of the Bash If Not condition. We hope these tips will help you understand the concept better. Regarding similar Bash-related issues (such as sleeping in Bash scripts), browse our website for more guidance.
Leave a comment