1. 算术运算符:
- 加法: +
result=$((num1 + num2))
- 减法: -
result=$((num1 - num2))
- 乘法: *
result=$((num1 * num2))
- 除法: /
result=$((num1 / num2))
- 取余: %
result=$((num1 % num2))
2. 关系运算符:
- 等于: ==
if [ $num1 == $num2 ]; then
# commands
fi
- 不等于: !=
if [ $num1 != $num2 ]; then
# commands
fi
- 大于: >
if [ $num1 > $num2 ]; then
# commands
fi
- 小于: <
if [ $num1 < $num2 ]; then
# commands
fi
- 大于等于: -ge
if [ $num1 -ge $num2 ]; then
# commands
fi
- 小于等于: -le
if [ $num1 -le $num2 ]; then
# commands
fi
3. 逻辑运算符:
- 与: &&
if [ $condition1 ] && [ $condition2 ]; then
# commands
fi
- 或: ||
if [ $condition1 ] || [ $condition2 ]; then
# commands
fi
- 非: !
if ! [ $condition ]; then
# commands
fi
4. 字符串运算符:
- 等于: =
if [ "$str1" = "$str2" ]; then
# commands
fi
- 不等于: !=
if [ "$str1" != "$str2" ]; then
# commands
fi
- 字符串长度: ${#string}
length=${#my_string}
- 字符串为空: -z
if [ -z "$str" ]; then
# commands
fi
- 字符串不为空: -n
if [ -n "$str" ]; then
# commands
fi
这些是一些基本的Shell运算符,可以用于执行算术、关系、逻辑和字符串操作。在编写Shell脚本时,了解这些运算符将帮助你更灵活地处理各种运算。
转载请注明出处:http://www.zyzy.cn/article/detail/3284/Linux