概述
实现shell脚本中,或者如我们使用linux的其他命令中,选项是常见的需要。
shell中获取参数可以直接使用$1、$2等形式来获取,但这种方式有明显的限制:每个参数的位置是固定的。比如如果在设计上 $1是用户名,$2是密码,因此执行时就必须第一个参数是用户名,第二个参数是密码而不能反过来。
shell提供了getopt和getopts来解析参数,getopt比getopts功能强一些
getopt介绍
getopt能在命令行中单独使用、支持长选项格式、支持选项值可选。
要使用getopt功能分成如下三个步骤:
定义命令执行选项
- 通过
-o m:t:c
声明单字符选项 - 通过–long para1:,para2:,para3,para4 声明长参数
其中针对冒号:,意义如下:
- 参数后面不加冒号(c)表示后面不接参数值, eg:-c,–create_result_dir
- 参数后面加冒号(m:)表示后面一定要接参数 eg:-m oracle,–mode oracle
- 参数后面加两个冒号(a::)表示后面参数可接可不接
if ! ARGS=$(getopt -o m:t:c --long mode:,testset:,create_result_dir,help -n "$0" -- "$@"); then
log error "Terminating..."
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "Options:\n --mode, -m: 指定本次测试的模式\n --create_result_dir, -c: 本脚本只是创建测试需要结果目录,不执行测试 \n --testset, -t: 指定运行特定的某个测试集,如login_test, user_test等 \n\n --help, -h 帮助"
exit 1
fi
将规范化后的命令行参数分配至位置参数($1,$2,…)
# The -- ensures that whatever options passed in as part of the script won't get interpreted as options for set, but as options for the command denoted by the $progname variable.
eval set -- "${ARGS}"
export create_result_dir="false"
export mode="mode1"
export testset=""
处理执行选项赋值给变量
while true; do
case "$1" in
-c|--create_result_dir)
create_result_dir='true'
shift
;;
-m|--mode)
case ${2,} in
"oracle")
mode="oracle mode"
shift 2
;;
"mysql")
mode="mysql mode"
shift 2
;;
*)
mode="oracle mode"
shift 2
;;
esac
;;
-t|--testset)
testset=$2
shift 2
;;
--)
shift
break
;;
-h|--help)
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "简介:这是一个单独执行sql特定测试集的辅助脚本xxxx\n"
echo -e "Options:\n --mode, -m: 指定本次测试的模式\n --create_result_dir, -c: 本脚本只是创建测试需要结果目录,不执行测试 \n --testset, -t: 指定运行特定的某个测试集,如login_test, user_test等 \n\n --help, -h 帮助"
exit 0
;;
?)
echo "missing options, pls check!"
exit 1
;;
esac
done
完整示例
上面三部分加起来就是完整可执行的完整示例
# 定义命令执行选项
# 参数后面不加冒号(c)表示后面不接参数 eg:-c,--create_result_dir
# 参数后面加冒号(m:)表示后面一定要接参数 eg:-m oracle,--mode oracle
# 参数后面加两个冒号(a::)表示后面参数可接可不接
if ! ARGS=$(getopt -o m:t:c --long mode:,testset:,create_result_dir,help -n "$0" -- "$@"); then
log error "Terminating..."
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "Options:\n --mode, -m: 指定本次测试的模式\n --create_result_dir, -c: 本脚本只是创建测试需要结果目录,不执行测试 \n --testset, -t: 指定运行特定的某个测试集,如login_test, user_test等 \n\n --help, -h 帮助"
exit 1
fi
# 将规范化后的命令行参数分配至位置参数($1,$2,...)
# The -- ensures that whatever options passed in as part of the script won't get interpreted as options for set, but as options for the command denoted by the $progname variable.
eval set -- "${ARGS}"
export create_result_dir="false"
export mode="oracle"
export testset=""
# 接受执行选项;赋值给变量
while true; do
case "$1" in
-c|--create_result_dir)
create_result_dir='true'
shift
;;
-m|--mode)
case ${2,} in
"oracle")
mode="oracle mode"
shift 2
;;
"mysql")
mode="mysql mode"
shift 2
;;
*)
mode="oracle mode"
shift 2
;;
esac
;;
-t|--testset)
testset=$2
shift 2
;;
--)
shift
break
;;
-h|--help)
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "简介:这是一个单独执行sql特定测试集的辅助脚本\n"
echo -e "Options:\n --mode, -m: 指定本次测试的模式\n --create_result_dir, -c: 本脚本只是创建测试需要结果目录,不执行测试 \n --testset, -t: 指定运行特定的某个测试集,如login_test, user_test等 \n\n --help, -h 帮助"
exit 0
;;
?)
echo "missing options, pls check!"
exit 1
;;
esac
done