General structure
if [ expression ]; then
else
fi
or
if [ expression ]; then
fi
As you can see, it starts with "if", followed by the comparison, then "; then", and starting from the next line — the operations to execute if the comparison succeeds.
If you need to add "else" — write it on a separate line, after which, from the next line — describe what needs to be done if the comparison is not true.
All of this ends with the line "fi".
Note. A common mistake is omitting spaces. SH is very particular about this, and a construct like
if ["$a"!="$b"]; then
fi
won't work!
Here are a few examples:
a="value1"
b="value2"
if [ "$a" = "$b" ]; then
echo "a equals b"
else
echo "a is not equal to b"
fi
if [ -f "/home/user/thefile" ]; then
echo "File found in user's folder"
fi
a="value1"
if [ "$a" != "anothervalue" ]; then
echo "variable a is not equal to 'anothervalue'"
fi
"AND" and "OR" structuresFor this, the operators "-a" and "-o" are used for "and" and "or" respectively.
if [ "$a" = "$b" -a "$c" = "$d" ]; then
echo "Both expressions are true"
fi
if [ "$a" = "$b" -o "$c" = "$d" ]; then
echo "One of the expressions is true"
fi
Negating the resultFor this, an exclamation mark is used, placed before the comparison (!):
if [ ! -f "/home/user/file" ]; then
echo "File not found"
fi
Now a bit about comparison operators=String equality (not numbers!). So, "0" = "0" is true, but "0" = "00" is already false!
if [ "$a" = "mystring" ]; then
echo "equal"
fi
ATTENTION! You cannot use a construct like this:
if [ "$a" = "" ]; then
echo "This is an error! Such a script won't work!"
fi
you need to do it like this:
if [ -z "$a" ]; then
echo "Variable a is an empty string of zero length"
fi
!=String inequality (not numbers!).
if [ "$a" != "mystring" ]; then
echo "not equal"
fi
ATTENTION! You cannot use a construct like this:
if [ "$a" != "" ]; then
echo "This is an error! such a script will not work!"
fi
you need to do it like this:
if [ -n "$a" ]; then
echo "Variable a is NOT an empty string of zero length"
fi
-eqNumeric equality. So, "01" -eq "1" - will be true.
a=123
b=0123
if [ "$a" -eq "$b" ]; then
echo "They are equal!"
fi
-neNumeric inequality.
a=123
b=234
if [ "$a" -ne "$b" ]; then
echo "Variable a is not equal to b - numeric comparison"
fi
-gt and
-geGreater than (-gt); greater than or equal to (-ge) - numeric comparison.
a=5
b=10
if [ "$b" -gt "$a" ]; then
echo "Variable b is greater than a"
fi
a=5
b=10
if [ "$b" -ge "$a" ]; then
echo "Variable b is greater than or equal to a"
fi
-lt and
-leLess than (-lt); less than or equal to (-le) - numeric comparison.
a=5
b=10
if [ "$a" -lt "$b" ]; then
echo "Variable a is less than b"
fi
a=5
b=10
if [ "$a" -le "$b" ]; then
echo "Variable a is less than or equal to b"
fi
<String comparison. Alphabetically (ASCII) less than.
a="A"
b="B"
if [[ "$a" < "$b" ]]; then
echo "ASCII of variable a is less than b"
fi
Note that here we escape the "<" sign using double brackets "[[" and "]]"!
another way to write this (without double quotes):
if [ "$a" \< "$b" ]; then
>String comparison. Alphabetically (ASCII) greater than.
a="A"
b="B"
if [[ "$a" > "$b" ]]; then
echo "ASCII of variable a is greater than b"
fi
Note that here we escape the ">" sign using double brackets "[[" and "]]"!
another way to write this (without double quotes):
if [ "$a" \< "$b" ]; then
-zTrue if the string is empty (has zero length).
a=""
if [ -z "$a" ]; then
echo "Variable a is empty"
fi
-nTrue if the string is not empty.
a="something"
if [ -n "$a" ]; then
echo "The string 'a' is not empty"
fi
-fFile existence
if [ -f "/home/user/myfile" ]; then
echo "File found"
fi
-dDirectory existence
if [ -d "/home/user/directory" ]; then
echo "Directory found"
fi
Comments