官术网_书友最值得收藏!

  • 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
主站蜘蛛池模板: 平遥县| 大足县| 宁化县| 宜州市| 庆云县| 富阳市| 梅河口市| 阜康市| 沧源| 栖霞市| 滨海县| 阳山县| 鄂托克前旗| 裕民县| 大石桥市| 明星| 赤峰市| 泸溪县| 库尔勒市| 土默特左旗| 澳门| 四平市| 乡城县| 德惠市| 常山县| 孟津县| 来宾市| 夏邑县| 蕉岭县| 武胜县| 吴堡县| 乌恰县| 手游| 西宁市| 定州市| 出国| 汶川县| 揭阳市| 五大连池市| 肃北| 蓝山县|