[内容] Linux case 条件判断

内容一:case 语句的格式

case <variable> in
<variable value 1>)
<the command to execute when the variable is this value>;;
<variable value 2>)
<the command to execute when the variable is this value>;;
<variable value 3>)
<the command to execute when the variable is this value>;;
*)
<the command to execute when the variable value is other>;;
esca

内容二:case 语句的使用案例

2.1 直接使用 case 语句的案例

#!/bin/bash

read -p "Which one do you like better, eternalcenter eternalcentre ec-x : " name

case $name in
eternalcenter)
    echo "Do you realy like $name better? eternalcenter?" ;;
eternalcentre)
    echo "Do you realy like $name better? eternalcentre?" ;;
ec-x)
    echo "Do you realy like $name better? ec-x?" ;;
*)
    echo "So you don't like them all ." ;;
esac

2.2 用变量作为条件判断使用 case 语句的案例

#!/bin/bash

read -p "Which one do you like better, eternalcenter eternalcentre ec-x : " name

Variable1=eternalcenter
Variable2=eternalcentre
Variable3=ec-x

case $name in
$Variable1)
    echo "Do you realy like $name better? eternalcenter?" ;;
$Variable2)
    echo "Do you realy like $name better? eternalcentre?" ;;
$Variable3)
    echo "Do you realy like $name better? ec-x?" ;;
*)
    echo "So you don't like them all ." ;;
esac