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

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).
主站蜘蛛池模板: 黄骅市| 四川省| 宁南县| 柏乡县| 平湖市| 蓬溪县| 秦安县| 北京市| 沁源县| 湘潭市| 兴城市| 即墨市| 定远县| 吉林省| 锦屏县| 穆棱市| 封丘县| 乌兰县| 建湖县| 庆阳市| 鄂尔多斯市| 垣曲县| 惠来县| 六盘水市| 台北市| 拜泉县| 花莲县| 汾阳市| 阿巴嘎旗| 象山县| 通州区| 营口市| 庆安县| 蚌埠市| 金寨县| 古田县| 鄂州市| 诸城市| 开化县| 石河子市| 德江县|