- 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).
推薦閱讀
- 64位匯編語言的編程藝術
- aelf區塊鏈應用架構指南
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- 深入理解Elasticsearch(原書第3版)
- The DevOps 2.5 Toolkit
- Python Web數據分析可視化:基于Django框架的開發實戰
- 低代碼平臺開發實踐:基于React
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Mastering C++ Multithreading
- 零基礎學C語言程序設計
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- QlikView Unlocked
- Java高并發編程詳解:深入理解并發核心庫
- Scrapy網絡爬蟲實戰
- Offer來了:Java面試核心知識點精講(框架篇)