case结构

case结构
case value in
    pattern1)
            command
            command;;
    pattern2)
            command
            command;;
    ...
    patternn)
            command;
esac
* case语句只执行第一个匹配模式
例子1:
echo "9-------------------------------"
#Display a menu
echo _
echo "1 Restore"
echo "2 Backup"
echo "3 Unload"
#Read and excute the user's selection
echo "Enter Choice:"
read CHOICE
case "$CHOICE" in
    1) echo "Restore";;
    2) echo "Backup";;
    3) echo "Unload";;
    *) echo "Sorry $CHOICE is not a valid choice";
exit 1
esac

*指默认匹配动作此外case模式中也可以使用逻辑操作

pattern1 | pattern2 )
    command
    command ;;
例子2:
case "$CHOICE" in
    1|R) echo "Restore";;
    2|B) echo "Backup";;
    3|U) echo "Unload";;
    *) echo "Sorry $CHOICE is not a valid choice"
exit 1
esac

你可能感兴趣的:(shell)