- Bash Cookbook
- Ron Brash Ganesh Naik
- 145字
- 2021-07-23 19:17:30
Basic case statement
Instead of if/else statements, you can use case statements to evaluate a variable. Notice that esac is case backwards and is used to exit the case statement similar to fi for if statements.
Case statements follow this flow:
case $THING_I_AM_TO_EVALUATE in 1) # Condition to evaluate is number 1 (could be "a" for a string too!)
echo "THING_I_AM_TO_EVALUATE equals 1"
;; # Notice that this is used to close this evaluation
*) # * Signified the catchall (when THING_I_AM_TO_EVALUATE does not equal values in the switch)
echo "FALLTHOUGH or default condition"
esac # Close case statement
The following is a working example:
#!/bin/bash
VAR=10 # Edit to 1 or 2 and re-run, after running the script as is.
case $VAR in 1)
echo "1"
;;
2)
echo "2"
;;
*)
echo "What is this var?"
exit 1 esac
推薦閱讀
- C#完全自學教程
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- Visual FoxPro 程序設計
- 區塊鏈架構與實現:Cosmos詳解
- Python機器學習實戰
- Mastering macOS Programming
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Serverless架構
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Yii Project Blueprints
- 編程菜鳥學Python數據分析
- OpenCV with Python By Example
- 從Excel到Python數據分析:Pandas、xlwings、openpyxl、Matplotlib的交互與應用
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- LabVIEW數據采集