当前位置:网站首页>shell脚本使用两个横杠接收外部参数
shell脚本使用两个横杠接收外部参数
2022-07-01 03:09:00 【Saggitarxm】
首先,效果是这样的:
既可以处理短选项(-)又可以处理长选项(--)
[[email protected] shell]$ ./demo.sh --help
sqoop程序开始运行: demo.sh
Usage: ./demo.sh [options]
Options:
--append, -a: 追加导入(默认为追加模式)
--overwrite, -o: 覆盖导入
--method, -m: single-单日导入
interval-区间导入
all-全表导入
--date, -d: 单日导入,某一日期数据(格式为yyyymmdd)
--startdate, -s: 区间导入,开始日期
--enddate, -e: 区间导入,结束日期
--help, -h 帮助
shell脚本接外部参数有一种很简单的办法,在脚本中使用$0,$1,$2...指代执行脚本时传入的第几个参数($0是脚本名)。
但是,这样做毕竟不够优雅,
另一种方法shell脚本内使用getopts命令,只可以接短选项(eg:-d,-s,-h),很方便,比较简单,可以自己去搜一搜。
但如果想要达成上面这种效果同时支持长选项和短选项(eg:--date,-d,--startdate,-s,--help,-h),
就只能使用getopt命令了:
# 定义命令执行选项
# 参数后面不加冒号(a)表示后面不接参数 eg:-a,--append
# 参数后面加冒号(a:)表示后面一定要接参数 eg:-d 20201224,--date 20201224
# 参数后面加两个冒号(a::)表示后面参数可接可不接
if ! ARGS=$(getopt -o aom:d:s:e:h --long append,overwrite,method:,date:,startdate:,enddate:,help -n "$0" -- "[email protected]"); then
echo "Terminating..."
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "Options:\n --append, -a: 追加导入(默认为追加模式)\n --overwrite, -o: 覆盖导入 \n\n --method, -m: single-单日导入\n interval-区间导入\n all-全表导入\n\n --date, -d: 单日导入,某一日期数据(格式为yyyymmdd)\n\n --startdate, -s: 区间导入,开始日期\n --enddate, -e: 区间导入,结束日期\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}"
# 接受执行选项;赋值给变量
while true; do
case "$1" in
-a|--append)
mode='append'
shift
;;
-o|--overwrite)
mode='overwrite'
shift
;;
-m|--method)
method=$2
shift 2
;;
-d|--date)
date=$2
shift 2
;;
-s|--startdate)
startdate=$2
shift 2
;;
-e|--enddate)
enddate=$2
shift 2
;;
--)
shift
break
;;
-h|--help)
echo -e "Usage: ./$SCRIPT_NAME [options]\n"
echo -e "Options:\n --append, -a: 追加导入(默认为追加模式)\n --overwrite, -o: 覆盖导入 \n\n --method, -m: single-单日导入\n interval-区间导入\n all-全表导入\n\n --date, -d: 单日导入,某一日期数据(格式为yyyymmdd)\n\n --startdate, -s: 区间导入,开始日期\n --enddate, -e: 区间导入,结束日期\n\n --help, -h 帮助"
exit 0
;;
?)
echo "missing options, pls check!"
exit 1
;;
esac
done
边栏推荐
- C#实现图的深度优先遍历--非递归代码
- Huawei operator level router configuration example | configuration static VPLS example
- Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
- Golang多图生成gif
- Introduction and installation of Solr
- C language EXECL function
- Error accessing URL 404
- Introduction to ieda right click source file menu
- VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
- Redis efficient like and cancel function
猜你喜欢
How to verify whether the contents of two files are the same
MCU firmware packaging Script Software
Complete training and verification of a neural network based on pytorch
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
【机器学习】向量化计算 -- 机器学习路上必经路
Feign远程调用和Getaway网关
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
8 pits of redis distributed lock
C语言多线程编程入门学习笔记
随机推荐
Force buckle - sum of two numbers
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
EDLines: A real-time line segment detector with a false detection control翻译
Kmeans
xxl-job使用指南
Big orange crazy blog move notice
雪崩问题以及sentinel的使用
If a parent class defines a parameterless constructor, is it necessary to call super ()?
Druid monitoring statistics source
[exsi] transfer files between hosts
Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
Saving images of different depths in opencv
JUC learning
EtherCAT简介
pytest-fixture
Network address translation (NAT) technology
js 找出两个数组中的重复元素
MySQL index --01--- design principle of index
php批量excel转word
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]