官术网_书友最值得收藏!

Case/switch statements and loop constructs

Besides if and else statements, Bash offers case or switch statements and loop constructs that can be used to simplify logic so that it is more readable and sustainable. Imagine creating an if statement with many elif evaluations. It would become cumbersome!

#!/bin/bash
VAR=10

# Multiple IF statements
if [ $VAR -eq 1 ]; then
echo "$VAR"
elif [ $VAR -eq 2]; then
echo "$VAR"
elif [ $VAR -eq 3]; then
echo "$VAR"
# .... to 10
else
echo "I am not looking to match this value"
fi
In a large number of blocks of conditional logic of if and elifs, each if and elif needs to be evaluated before executing a specific branch of code. It can be faster to use a case/switch statement, because the first match will be executed (and it looks prettier).
主站蜘蛛池模板: 漳浦县| 武定县| 开化县| 靖边县| 垣曲县| 文登市| 桑日县| 石门县| 周至县| 上杭县| 区。| 娱乐| 哈巴河县| 武强县| 临洮县| 芜湖县| 哈巴河县| 神木县| 东莞市| 清丰县| 鞍山市| 曲靖市| 五河县| 鹿邑县| 四平市| 漳浦县| 皋兰县| 莎车县| 德江县| 南部县| 张家界市| 德令哈市| 新闻| 额济纳旗| 惠来县| 喜德县| 衡南县| 木里| 元谋县| 阜康市| 高安市|