在Shell脚本中,嵌套if语句是指在一个if语句内部再包含一个或多个if语句。这种结构通常用于处理更复杂的条件逻辑。以下是一个嵌套if语句的示例:
#!/bin/bash num=10 if [ $num -gt 0 ]; then echo "Number is greater than 0" if [ $num -eq 1 ]; then echo "Number is equal to 1" else echo "Number is greater than 1 but not equal to 10" fi else echo "Number is not greater than 0" fi
在这个示例中,我们首先检查num
是否大于0。如果是,我们进入第一个if语句内部,然后检查num
是否等于1。如果等于1,我们输出"Number is equal to 1";否则,我们输出"Number is greater than 1 but not equal to 10"。如果num
不大于0,我们输出"Number is not greater than 0"。