如果仅有if,标准语法:if [ 表达式 ]; then... fi
#!/bin/bash
if [ "$#" -gt 0 ]
then
echo "There's Beans"
fi
if [ "$1" = "cool" ]
then
echo "Cool Beans"
fi
如果需要else:
#!/bin/bash
if [ "$1" = "cool" ]
then
echo "Cool Beans"
else
echo "Not Cool Beans"
fi
更一进步的,添加else if:
#!/bin/bash
if [ "$1" = "cool" ]
then
echo "Cool Beans"
elif [ "$1" = "neat" ]
then
echo "Neato cool"
else
echo "Not Cool Beans"
fi
if 语句所使用的条件表达式总结如下:
String Comparison | Description |
---|---|
Str1 = Str2 | Returns true if the strings are equal |
Str1 != Str2 | Returns true if the strings are not equal |
-n Str1 | Returns true if the string is not null |
-z Str1 | Returns true if the string is null |
Numeric Comparison | Description |
---|---|
expr1 -eq expr2 | Returns true if the expressions are equal |
expr1 -ne expr2 | Returns true if the expressions are not equal |
expr1 -gt expr2 | Returns true if expr1 is greater than expr2 |
expr1 -ge expr2 | Returns true if expr1 is greater than or equal to expr2 |
expr1 -lt expr2 | Returns true if expr1 is less than expr2 |
expr1 -le expr2 | Returns true if expr1 is less than or equal to expr2 |
!expr1 | Negates the result of expression |
File Conditionals | Description |
---|---|
-d file | True if the file is a directory |
-e file | True if the file exists (note that this is not particularly portable, thus -f is generally used) |
-f file | True if the provided string is a file |
-g file | True if the group id is set on a file |
-r file | True if the file is readable |
-s file | True if the file has a non-zero size |
-u | True if the user id is set on a file |
-w | True if the file is writable |
-x | True if the file is an executable |