dfstandish
dfstandish

Reputation: 134

simple numerical compare in zsh function always true

I want to do something randomly in my shell. I've boiled my issue down to this embarrassingly simple thing:

dice-roll() {
    local THRESHOLD=50
    #local DICE_ROLL=$[${RANDOM}%100]
    local DICE_ROLL=40

    if  ((DICE_ROLL -ge THESHOLD)); then
        echo "win: threshold is $THRESHOLD , rolled $DICE_ROLL"
    else
        echo "loss: rolled $DICE_ROLL"
    fi
}

I always win. Why?

Upvotes: 1

Views: 261

Answers (1)

user1934428
user1934428

Reputation: 22356

Wrong syntax, plus typing error in the variable name. It should be

 ((DICE_ROLL >= THRESHOLD))

or

[[ DICE_ROLL -ge THRESHOLD ]]

Upvotes: 4

Related Questions