- 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).
推薦閱讀
- Learning RxJava
- 華為HMS生態與應用開發實戰
- SQL for Data Analytics
- PhoneGap Mobile Application Development Cookbook
- Java 9模塊化開發:核心原則與實踐
- Python忍者秘籍
- Mastering JavaScript Design Patterns(Second Edition)
- AppInventor實踐教程:Android智能應用開發前傳
- 精通Python自動化編程
- Android Wear Projects
- Node.js:來一打 C++ 擴展
- Java語言程序設計教程
- ArcGIS for Desktop Cookbook
- PHP 7從零基礎到項目實戰
- Modernizing Legacy Applications in PHP