Shell 中的 if 语句非常灵活,主要有以下几种写法:

1. 最基本的单行形式 (Single Line)

这种形式适用于简单的条件判断,将所有内容写在一行,用分号 ; 分隔。

语法:

if [ condition ]; then commands; fi

示例:

if [ -f "myfile.txt" ]; then echo "File exists."; fi

你也可以把 then 和 commands 放在同一行:

if [ -f "myfile.txt" ]; then echo "File exists."; fi

2. 多行标准形式 (Multiline - Standard)

这是最常见、可读性最好的形式,用于复杂的脚本。

语法:

if [ condition ]
then
    # 条件为真时执行的命令
    command1
    command2
fi

示例:

if [ "$USER" = "root" ]
then
    echo "You are the superuser."
    systemctl status nginx
fi

3. 带有 else 分支

语法:

if [ condition ]
then
    # 条件为真时执行
    commands
else
    # 条件为假时执行
    other_commands
fi

示例:

if [ -d "/tmp" ]
then
    echo "/tmp is a directory."
else
    echo "/tmp is not a directory."
fi

4. 带有 elif 的多条件判断 (Else If)

语法:

if [ condition1 ]
then
    # 条件1为真时执行
    commands
elif [ condition2 ]
then
    # 条件2为真时执行
    commands
else
    # 所有条件都为假时执行
    commands
fi

示例:

if [ "$1" -gt "100" ]
then
    echo "Argument is greater than 100."
elif [ "$1" -eq "100" ]
then
    echo "Argument is exactly 100."
else
    echo "Argument is less than 100."
fi

5. 使用 test 命令或 [[ ... ]]

[ ... ] 实际上是 test 命令的另一种形式。在现代 Shell (如 Bash, Zsh) 中,更推荐使用 [[ ... ]],因为它更强大且更安全(能处理空变量和特殊字符)。

使用 [[ 的示例:

if [[ $name == "Alice" ]]
then
    echo "Hello, Alice!"
fi

# [[ 支持正则表达式匹配
if [[ "$file" == *.txt ]]
then
    echo "This is a text file."
fi

6. 直接在 if 后使用命令

if 语句的本质是判断其后命令的退出状态码。如果状态码为 0(表示成功),则条件为真;非 0 则表示失败,条件为假。因此,你可以直接使用任何命令。

语法:

if command; then
    # 如果 command 执行成功 (返回 0)
    commands
fi

示例:

# 检查命令是否存在
if command -v git &> /dev/null
then
    echo "Git is installed."
fi

# 检查上一个命令是否成功
mkdir /some/dir
if [ $? -eq 0 ]
then
    echo "Directory created successfully."
fi

# 更简洁的写法,直接将命令放在if后
if mkdir /some/dir; then
    echo "Directory created successfully."
fi

# 检查进程是否存在
if pgrep -x "nginx" > /dev/null
then
    echo "Nginx is running."
fi

7. 与逻辑操作符结合 (&&||)

对于非常简单的“成功就执行”的情况,可以使用逻辑操作符代替 if 语句,这更简洁。

相当于 if-then

# 如果前一个命令成功,则执行下一个
[ -f "myfile.txt" ] && echo "File exists."

# 等同于
if [ -f "myfile.txt" ]; then echo "File exists."; fi

相当于 if-then-else

# 如果前一个命令成功,执行 command1,否则执行 command2
[ -f "myfile.txt" ] && echo "File exists." || echo "File does not exist."

注意:这种 A && B || C 的结构并不完全等同于 if A; then B; else C; fi。如果命令 B 执行失败(返回非0),C 也会被执行。所以只建议在 B 绝对不会失败的情况下使用。


条件判断常用运算符

最后,附上在 [ ] 或 [[ ]] 中最常用的比较运算符:

 
 
类型 运算符 描述 示例
字符串比较 = 或 == 相等 [ "$a" = "$b" ]
  != 不相等 [ "$a" != "$b" ]
  -z 字符串长度为0(空) [ -z "$var" ]
  -n 字符串长度非0(非空) [ -n "$var" ]
数字比较 -eq 等于 (Equal) [ "$a" -eq "$b" ]
  -ne 不等于 (Not Equal) [ "$a" -ne "$b" ]
  -gt 大于 (Greater Than) [ "$a" -gt "$b" ]
  -ge 大于等于 (Greater or Equal) [ "$a" -ge "$b" ]
  -lt 小于 (Less Than) [ "$a" -lt "$b" ]
  -le 小于等于 (Less or Equal) [ "$a" -le "$b" ]
文件测试 -e 文件/目录存在 [ -e "path" ]
  -f 存在且是普通文件 [ -f "file" ]
  -d 存在且是目录 [ -d "dir" ]
  -r 文件可读 [ -r "file" ]
  -w 文件可写 [ -w "file" ]
  -x 文件可执行 [ -x "file" ]
  -s 文件存在且非空 [ -s "file" ]

总结:

  • 简单判断或脚本开头检查用单行形式

  • 脚本中的复杂逻辑用多行标准形式,可读性最好。

  • 检查命令是否成功,直接把命令放在 if 后面

  • 非常简单的分支可以用逻辑操作符 && 和 || 简化。

posted on 2025-09-12 10:15  天外来客I  阅读(32)  评论(0)    收藏  举报