Bash if statement with multiple conditions throws an error

前端 未结 4 2113
慢半拍i
慢半拍i 2020-11-30 18:20

I\'m trying to write a script that will check two error flags, and in case one flag (or both) are changed it\'ll echo-- error happened. My script:

my_error_f         


        
4条回答
  •  渐次进展
    2020-11-30 18:42

    You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq, -lt. I think, (( is most preferred for arithmetic, because you can directly use operators such as ==, < and >.

    Using [[ operator

    a=$1
    b=$2
    if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
    then
         echo "Error"
    else
         echo "No Error"
    fi
    

    Using (( operator

    a=$1
    b=$2
    if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
    then
         echo "Error"
    else
         echo "No Error"
    fi
    

    Do not use -a or -o operators Since it is not Portable.

提交回复
热议问题