|
if then else in BASH - the basics |
|
|
programming
|
If then else in bash is not very difficult. However, there is something that can cause problems: Please note the spacing inside the brackets! It won't work without! string comparison
simple example:
#!/bin/bash
MYVAR="hello" if [ "$MYVAR" = "hello" ]; then echo "true" else echo "false" fi
integer comparison
A is equal B - a simple example:
#!/bin/bash
A=1
B=2
if [ "$A" -eq "$B" ]; then echo "$A is queal $B" else echo "$A is not equal $B" fi
A is not equal B - a simple example:
#!/bin/bash
A=1
B=2
if [ "$A" -ne "$B" ]; then echo "$A is not queal $B" else echo "$A is equal $B" fi
A is greater than B - a simple example:
#!/bin/bash
A=2
B=1
if [ "$A" -gt "$B" ]; then echo "$A is greater than $B" else echo "$A is not greater than $B" fi
A is less than B - a simple example:
#!/bin/bash
A=1
B=2
if [ "$A" -lt "$B" ]; then echo "$A is less than $B" else echo "$A is not less than $B" fi
A is less than or equal to B - a simple example:
#!/bin/bash
A=1
B=2
if [ "$A" -le "$B" ]; then echo "$A is less than or equal to $B" else echo "$A is greater than $B" fi
|
I am new to Linux programming.
the above examples looks good thanks very much
From
kannan.R