3.05 if与结合表达式

if与结合表达式 #

结合表达式常见类型有以下几种

表达式 含义
&& 与操作
|| 或操作
! 否操作

&&
脚本说明

#!/bin/bash

a=1
b=10
c=5
if [[ ${c} =~ ^-?[0-9]+$ ]];then
    if [[ ${c} -ge ${a} && ${c} -le ${b} ]];then
        echo "${c} is within $a and $b ."   
    else
        echo "${c} is out of range."
    fi
else
    echo "${c} is not a integer."
fi

||
脚本说明

#!/bin/bash

a=1
b=10
c=5
if [[ ${c} =~ ^-?[0-9]+$ ]];then
    if [[ ${c} -lt ${a} || ${c} -gt ${b} ]];then
        echo "${c} is out of range."   
    else
        echo "${c} is within ${a} and ${b}."
    fi
else
    echo "${c} is not a integer."
fi

!
脚本说明

#!/bin/bash

a=1
b=10
c=5
if [[ ${c} =~ ^-?[0-9]+$ ]];then
    if [[ ! \( ${c} -ge ${a} && ${c} -le ${b} \) ]];then
        echo "${c} is out of range."   
    else
        echo "${c} is within ${a} and ${b}."
    fi
else
    echo "${c} is not a integer."
fi

在bash中>、<、( 、)与特殊含义,需要进行转义处理

⬅️上一页

下一页➡️

Calendar May 17, 2021
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者