Shell脚本--if

1. if 语句

语法格式为:

if condition
then
    statement(s)
fi

或者:

if condition ; then
    statement(s)
fi

condition 是判断条件,如果 condition 成立(返回“真”),那么 then 后边的语句将会被执行;如果 condition 不成立(返回“假”),那么不会执行任何语句。

示例:

#!/bin/bash

a=1
b=1
if (( a == b )); then
    echo "a和b相等"
fi

2. if else 语句

语法格式为:

if  condition ; then
   statement1
else
   statement2
fi

3. if elif else 语句

语法格式为:

if  condition1 ; then
   statement1
elif condition2 ; then
    statement2
else
   statementn
fi

 

 
posted @ 2023-04-18 15:42  时间在哪  阅读(64)  评论(0)    收藏  举报