Appearance
条件判断
if 语句
基本语法
bash
if condition; then
command
fi简单示例
bash
#!/bin/bash
age=18
if [ $age -ge 18 ]; then
echo "已成年"
fiif-else 语句
基本语法
bash
if condition; then
command1
else
command2
fi示例
bash
#!/bin/bash
age=16
if [ $age -ge 18 ]; then
echo "已成年"
else
echo "未成年"
fiif-elif-else 语句
基本语法
bash
if condition1; then
command1
elif condition2; then
command2
else
command3
fi示例
bash
#!/bin/bash
score=85
if [ $score -ge 90 ]; then
echo "优秀"
elif [ $score -ge 80 ]; then
echo "良好"
elif [ $score -ge 60 ]; then
echo "及格"
else
echo "不及格"
fi嵌套 if
基本语法
bash
if condition1; then
if condition2; then
command
fi
fi示例
bash
#!/bin/bash
age=20
has_id=true
if [ $age -ge 18 ]; then
if [ "$has_id" = true ]; then
echo "可以进入"
else
echo "需要身份证"
fi
else
echo "未成年,不能进入"
fi条件测试
test 命令
bash
#!/bin/bash
age=18
# 使用 test 命令
if test $age -ge 18; then
echo "已成年"
fi[ ] 语法
bash
#!/bin/bash
age=18
# 使用 [ ] 语法
if [ $age -ge 18 ]; then
echo "已成年"
fi[[ ]] 语法
bash
#!/bin/bash
age=18
# 使用 [[ ]] 语法
if [[ $age -ge 18 ]]; then
echo "已成年"
fi文件测试
文件存在性测试
bash
#!/bin/bash
file="test.txt"
# 检查文件是否存在
if [ -e "$file" ]; then
echo "文件存在"
else
echo "文件不存在"
fi
# 检查是否为普通文件
if [ -f "$file" ]; then
echo "是普通文件"
fi
# 检查是否为目录
if [ -d "$file" ]; then
echo "是目录"
fi文件权限测试
bash
#!/bin/bash
file="test.txt"
# 检查是否可读
if [ -r "$file" ]; then
echo "文件可读"
fi
# 检查是否可写
if [ -w "$file" ]; then
echo "文件可写"
fi
# 检查是否可执行
if [ -x "$file" ]; then
echo "文件可执行"
fi文件大小测试
bash
#!/bin/bash
file="test.txt"
# 检查文件是否为空
if [ -s "$file" ]; then
echo "文件不为空"
else
echo "文件为空"
fi字符串测试
字符串比较
bash
#!/bin/bash
str1="hello"
str2="world"
# 检查字符串是否相等
if [ "$str1" = "$str2" ]; then
echo "字符串相等"
else
echo "字符串不相等"
fi
# 检查字符串是否不相等
if [ "$str1" != "$str2" ]; then
echo "字符串不相等"
fi字符串长度测试
bash
#!/bin/bash
str="hello"
# 检查字符串是否为空
if [ -z "$str" ]; then
echo "字符串为空"
fi
# 检查字符串是否不为空
if [ -n "$str" ]; then
echo "字符串不为空"
fi字典序比较
bash
#!/bin/bash
str1="apple"
str2="banana"
# 检查 str1 是否小于 str2
if [[ "$str1" < "$str2" ]]; then
echo "$str1 小于 $str2"
fi
# 检查 str1 是否大于 str2
if [[ "$str1" > "$str2" ]]; then
echo "$str1 大于 $str2"
fi数字比较
基本比较
bash
#!/bin/bash
a=10
b=20
# 等于
if [ $a -eq $b ]; then
echo "$a 等于 $b"
fi
# 不等于
if [ $a -ne $b ]; then
echo "$a 不等于 $b"
fi
# 大于
if [ $a -gt $b ]; then
echo "$a 大于 $b"
fi
# 小于
if [ $a -lt $b ]; then
echo "$a 小于 $b"
fi
# 大于等于
if [ $a -ge $b ]; then
echo "$a 大于等于 $b"
fi
# 小于等于
if [ $a -le $b ]; then
echo "$a 小于等于 $b"
fi使用双括号
bash
#!/bin/bash
a=10
b=20
# 使用双括号
if ((a < b)); then
echo "$a 小于 $b"
fi
if ((a > 5 && b < 30)); then
echo "条件成立"
fi逻辑运算
逻辑与
bash
#!/bin/bash
a=10
b=20
# 使用 -a
if [ $a -gt 5 -a $b -lt 30 ]; then
echo "条件成立"
fi
# 使用 &&
if [ $a -gt 5 ] && [ $b -lt 30 ]; then
echo "条件成立"
fi
# 使用 [[ ]]
if [[ $a -gt 5 && $b -lt 30 ]]; then
echo "条件成立"
fi逻辑或
bash
#!/bin/bash
a=10
b=20
# 使用 -o
if [ $a -gt 15 -o $b -lt 30 ]; then
echo "条件成立"
fi
# 使用 ||
if [ $a -gt 15 ] || [ $b -lt 30 ]; then
echo "条件成立"
fi
# 使用 [[ ]]
if [[ $a -gt 15 || $b -lt 30 ]]; then
echo "条件成立"
fi逻辑非
bash
#!/bin/bash
a=10
# 使用 !
if [ ! $a -eq 20 ]; then
echo "$a 不等于 20"
fi
# 使用 [[ ]]
if [[ ! $a -eq 20 ]]; then
echo "$a 不等于 20"
fi正则表达式匹配
基本匹配
bash
#!/bin/bash
str="hello123"
# 检查是否匹配正则表达式
if [[ "$str" =~ ^[a-z]+[0-9]+$ ]]; then
echo "匹配成功"
fi实用示例
bash
#!/bin/bash
email="test@example.com"
# 检查是否为有效的邮箱
if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "有效的邮箱"
else
echo "无效的邮箱"
fi实用示例
示例1:检查命令行参数
bash
#!/bin/bash
# 检查参数数量
if [ $# -eq 0 ]; then
echo "错误: 没有提供参数"
echo "使用方法: $0 <参数>"
exit 1
fi
# 检查参数是否为文件
if [ -f "$1" ]; then
echo "参数是文件: $1"
elif [ -d "$1" ]; then
echo "参数是目录: $1"
else
echo "参数既不是文件也不是目录: $1"
fi示例2:检查服务状态
bash
#!/bin/bash
service="nginx"
# 检查服务是否运行
if systemctl is-active --quiet "$service"; then
echo "$service 正在运行"
else
echo "$service 未运行"
echo "尝试启动 $service..."
systemctl start "$service"
# 再次检查
if systemctl is-active --quiet "$service"; then
echo "$service 启动成功"
else
echo "$service 启动失败"
fi
fi示例3:检查磁盘空间
bash
#!/bin/bash
# 获取根分区使用率
disk_usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
# 检查磁盘使用率
if [ $disk_usage -gt 90 ]; then
echo "警告: 磁盘使用率超过 90%: $disk_usage%"
elif [ $disk_usage -gt 80 ]; then
echo "注意: 磁盘使用率超过 80%: $disk_usage%"
else
echo "磁盘使用率正常: $disk_usage%"
fi示例4:检查网络连接
bash
#!/bin/bash
host="www.google.com"
# 检查网络连接
if ping -c 1 -W 2 "$host" &> /dev/null; then
echo "网络连接正常"
else
echo "网络连接失败"
fi最佳实践
1. 使用双引号
bash
# 好的做法
if [ -n "$name" ]; then
echo "$name"
fi
# 不好的做法
if [ -n $name ]; then
echo $name
fi2. 使用双括号进行算术比较
bash
# 好的做法
if ((a > b)); then
echo "$a 大于 $b"
fi
# 不好的做法
if [ $a -gt $b ]; then
echo "$a 大于 $b"
fi3. 使用 [[ ]] 进行字符串比较
bash
# 好的做法
if [[ "$str1" == "$str2" ]]; then
echo "相等"
fi
# 不好的做法
if [ "$str1" == "$str2" ]; then
echo "相等"
fi总结
条件判断的关键点:
- if 语句:
if condition; then ... fi - if-else 语句:
if condition; then ... else ... fi - if-elif-else 语句:
if condition1; then ... elif condition2; then ... else ... fi - 嵌套 if:在 if 语句中嵌套另一个 if 语句
- 文件测试:
-e,-f,-d,-r,-w,-x,-s - 字符串测试:
=,!=,-z,-n,<,> - 数字比较:
-eq,-ne,-gt,-lt,-ge,-le - 逻辑运算:
&&,||,!
下一节我们将学习循环的使用。