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

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
主站蜘蛛池模板: 旬邑县| 青浦区| 隆子县| 平果县| 克拉玛依市| 林甸县| 扶余县| 大邑县| 津南区| 通道| 古浪县| 昌吉市| 金塔县| 水富县| 恩平市| 汶川县| 赫章县| 桐梓县| 苍山县| 惠安县| 丹阳市| 宜君县| 伊川县| 芷江| 天峨县| 鄂尔多斯市| 道真| 夏邑县| 塘沽区| 镇坪县| 叙永县| 舒兰市| 边坝县| 莱州市| 盈江县| 敦化市| 桃江县| 遵化市| 宁波市| 日照市| 遂宁市|