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

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).
主站蜘蛛池模板: 崇明县| 宁远县| 忻城县| 永靖县| 万盛区| 昌都县| 忻城县| 广灵县| 玉溪市| 巴马| 房产| 炎陵县| 昌吉市| 镇平县| 炉霍县| 祁门县| 吉林省| 绍兴市| 五华县| 开封县| 蒙自县| 溆浦县| 东至县| 闽清县| 营山县| 民权县| 宣化县| 白河县| 平阴县| 西丰县| 土默特右旗| 德安县| 双牌县| 出国| 贡嘎县| 高州市| 运城市| 彰化市| 崇文区| 鹤壁市| 都江堰市|