- 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 Cython Programming(Second Edition)
- Java EE框架整合開發(fā)入門到實戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- iOS開發(fā)實戰(zhàn):從零基礎(chǔ)到App Store上架
- 技術(shù)領(lǐng)導力:程序員如何才能帶團隊
- Implementing Cisco Networking Solutions
- 零基礎(chǔ)學Java(第4版)
- Python數(shù)據(jù)分析(第2版)
- Web全棧工程師的自我修養(yǎng)
- Expert Data Visualization
- 低代碼平臺開發(fā)實踐:基于React
- Visual Basic程序設(shè)計上機實驗教程
- Microsoft Azure Storage Essentials
- Microsoft Dynamics AX 2012 R3 Financial Management
- Emgu CV Essentials
- 寫給程序員的Python教程