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

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).
主站蜘蛛池模板: 两当县| 兖州市| 茂名市| 岳普湖县| 米林县| 扶沟县| 太康县| 和平区| 宝鸡市| 开鲁县| 临沭县| 八宿县| 永泰县| 岗巴县| 闸北区| 富源县| 清水河县| 宝鸡市| 凉城县| 鄂托克前旗| 黄石市| 陇川县| 岐山县| 麻江县| 习水县| 那曲县| 武定县| 泾阳县| 万山特区| 潮州市| 永定县| 四平市| 彭州市| 县级市| 同仁县| 馆陶县| 高清| 定襄县| 惠水县| 黑河市| 乌鲁木齐市|