乐闻世界logo
搜索文章和话题

Shell 脚本中如何使用 if 语句和 case 语句进行条件判断?

3月6日 21:40

Shell 脚本中常用的条件判断语句包括 if 语句、case 语句和测试命令。

if 语句

基本语法

bash
if [ condition ]; then commands elif [ condition ]; then commands else commands fi

文件测试操作符

bash
[ -f file ] # 文件存在且为普通文件 [ -d dir ] # 目录存在 [ -e file ] # 文件或目录存在 [ -r file ] # 文件可读 [ -w file ] # 文件可写 [ -x file ] # 文件可执行 [ -s file ] # 文件大小大于零 [ -L file ] # 文件是符号链接

字符串测试操作符

bash
[ "$str1" = "$str2" ] # 字符串相等 [ "$str1" != "$str2" ] # 字符串不等 [ -z "$str" ] # 字符串为空 [ -n "$str" ] # 字符串非空

数值测试操作符

bash
[ $num1 -eq $num2 ] # 等于 [ $num1 -ne $num2 ] # 不等于 [ $num1 -gt $num2 ] # 大于 [ $num1 -lt $num2 ] # 小于 [ $num1 -ge $num2 ] # 大于等于 [ $num1 -le $num2 ] # 小于等于

逻辑操作符

bash
[ condition1 ] && [ condition2 ] # 逻辑与 [ condition1 ] || [ condition2 ] # 逻辑或 ! [ condition ] # 逻辑非 [ condition1 -a condition2 ] # 逻辑与(在 [] 内) [ condition1 -o condition2 ] # 逻辑或(在 [] 内)

if 语句示例

bash
#!/bin/bash # 检查文件是否存在 if [ -f "/etc/passwd" ]; then echo "File exists" else echo "File does not exist" fi # 检查数值 age=25 if [ $age -ge 18 ]; then echo "Adult" elif [ $age -ge 13 ]; then echo "Teenager" else echo "Child" fi # 复合条件 if [ -f "$1" ] && [ -r "$1" ]; then echo "File exists and is readable" fi # 使用 [[ ]] 进行更复杂的测试 if [[ $name == "John" || $name == "Jane" ]]; then echo "Welcome $name" fi

case 语句

基本语法

bash
case $variable in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac

case 语句示例

bash
#!/bin/bash # 简单的菜单选择 echo "Select an option:" echo "1. Start" echo "2. Stop" echo "3. Restart" read -p "Enter choice: " choice case $choice in 1) echo "Starting service..." ;; 2) echo "Stopping service..." ;; 3) echo "Restarting service..." ;; *) echo "Invalid choice" ;; esac # 使用模式匹配 file="document.txt" case $file in *.txt) echo "Text file" ;; *.jpg|*.png) echo "Image file" ;; *) echo "Unknown file type" ;; esac

测试命令

test 命令

bash
test condition # 等同于 [ condition ]

双括号 [[ ]]

bash
# [[ ]] 是 Bash 的扩展,功能更强大 [[ $name == "John" && $age -gt 18 ]]

双括号 (( ))

bash
# (( )) 用于算术运算 (( age++ )) if (( age >= 18 )); then echo "Adult" fi

实际应用示例

bash
#!/bin/bash # 检查命令行参数 if [ $# -eq 0 ]; then echo "Usage: $0 <filename>" exit 1 fi file=$1 # 检查文件类型 if [ -f "$file" ]; then echo "File exists" # 检查文件权限 if [ -r "$file" ]; then echo "File is readable" else echo "File is not readable" fi if [ -w "$file" ]; then echo "File is writable" else echo "File is not writable" fi elif [ -d "$file" ]; then echo "Directory exists" # 检查目录内容 file_count=$(ls -1 "$file" | wc -l) if [ $file_count -eq 0 ]; then echo "Directory is empty" else echo "Directory contains $file_count items" fi else echo "File or directory does not exist" exit 1 fi # 使用 case 处理文件扩展名 extension="${file##*.}" case $extension in txt|md) echo "Text document" ;; sh) echo "Shell script" ;; py) echo "Python script" ;; *) echo "Unknown file type" ;; esac

最佳实践

  1. 始终使用引号包裹变量: "$variable" 防止空格和特殊字符问题
  2. 优先使用 [[ ]] 而不是 []: 功能更强大,更安全
  3. 使用 case 处理多条件匹配: 代码更清晰
  4. 检查命令执行状态: 使用 $?if command; then
  5. 避免使用 -a 和 -o: 使用 &&|| 更清晰
标签:Shell