What command means “do nothing” in a conditional in Bash?

后端 未结 3 1685
孤独总比滥情好
孤独总比滥情好 2021-01-29 18:30

Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when $a is greater than \"10\", print \"1\" if $a

3条回答
  •  隐瞒了意图╮
    2021-01-29 19:18

    You can probably just use the true command:

    if [ "$a" -ge 10 ]; then
        true
    elif [ "$a" -le 5 ]; then
        echo "1"
    else
        echo "2"
    fi
    

    An alternative, in your example case (but not necessarily everywhere) is to re-order your if/else:

    if [ "$a" -le 5 ]; then
        echo "1"
    elif [ "$a" -lt 10 ]; then
        echo "2"
    fi
    

提交回复
热议问题