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

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).
主站蜘蛛池模板: 临清市| 卓资县| 拜城县| 张家港市| 永丰县| 调兵山市| 安徽省| 新泰市| 苏州市| 苗栗县| 梁山县| 望城县| 故城县| 哈巴河县| 九龙坡区| 和林格尔县| 外汇| 伊川县| 大港区| 洞口县| 大理市| 晋州市| 长子县| 浦北县| 平遥县| 盖州市| 靖江市| 嫩江县| 宜兴市| 黄冈市| 长垣县| 龙口市| 麻城市| 海伦市| 多伦县| 达尔| 荥阳市| 大安市| 无锡市| 双江| 定兴县|