Linux 中的 If

首页 / Linux入门教程 / Linux 中的 If

if.elif.fi语句是控制语句的一级高级形式,它允许Shell在多个条件中做出正确的决定。

If-elif - 语法

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

这段代码只是一系列if语句,其中每个if都是上一条语句的else子句的一部分。这里的语句是基于TRUE条件执行的,如果没有一个条件为TRUE,则执行else块。

无涯教程网

If-elif - 示例

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

执行后,您将收到以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/linux/linux-if-elif-statement.html

来源:LearnFk无涯教程网

a is less than b

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件工程之美 -〔宝玉〕

iOS开发高手课 -〔戴铭〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

TypeScript开发实战 -〔梁宵〕

系统性能调优必知必会 -〔陶辉〕

代码之丑 -〔郑晔〕

陈天 · Rust 编程第一课 -〔陈天〕

手把手教你落地DDD -〔钟敬〕

AI大模型系统实战 -〔Tyler〕

好记忆不如烂笔头。留下您的足迹吧 :)