- 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
推薦閱讀
- Flask Web全棧開發實戰
- Java逍遙游記
- Getting Started with Citrix XenApp? 7.6
- Linux C/C++服務器開發實踐
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- 華為HMS生態與應用開發實戰
- Python從入門到精通(精粹版)
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- PhoneGap:Beginner's Guide(Third Edition)
- Symfony2 Essentials
- ASP.NET Core 2 Fundamentals
- Python網絡爬蟲技術與應用
- Drupal Search Engine Optimization
- SAS編程演義
- Python繪圖指南:分形與數據可視化(全彩)