linux-tutorial/codes/shell/输入和输出/读取选择参数.sh

26 lines
891 B
Bash
Raw Normal View History

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