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

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 141字
  • 2021-07-23 19:17:30

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).
主站蜘蛛池模板: 广昌县| 榆社县| 全州县| 铁岭县| 茌平县| 吴桥县| 西贡区| 龙海市| 梅河口市| 郸城县| 宣威市| 乐至县| 鄂州市| 图们市| 七台河市| 白玉县| 华宁县| 微山县| 延津县| 利津县| 阳信县| 苍山县| 枣强县| 青岛市| 兴文县| 芜湖市| 云安县| 玉环县| 徐汇区| 中山市| 怀柔区| 武宁县| 会理县| 收藏| 定南县| 上蔡县| 临桂县| 大关县| 玛曲县| 盐城市| 洞口县|