linux-tutorial/codes/shell/输入和输出/读取选择参数.sh
2019-10-29 18:22:19 +08:00

26 lines
891 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
##############################################################
# 很多场景下,我们需要在执行脚本时,输入指定的参数,脚本会
# 根据参数执行不同的行为。这就需要读取脚本参数并校验。
##############################################################
################### 读取脚本输入参数并校验 ###################
declare -a serial
serial=( start stop restart )
echo -n "请选择操作可选值start|stop|restart"
read oper
if ! echo ${serial[@]} | grep -q ${oper}; then
echo "请选择正确操作可选值start|stop|restart"
exit 1
fi
declare -a serial2
serial2=( dev test prod )
echo -n "请选择运行环境可选值dev|test|prod"
read profile
if ! echo ${serial2[@]} | grep -q ${profile}; then
echo "请选择正确运行环境可选值dev|test|prod"
exit 1
fi