当前位置:网站首页>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边栏推荐
- Is it safe to open an account online in a small securities firm? Will my money be unsafe?
- POI导出excel,按照父子节点进行分级显示
- JS to find duplicate elements in two arrays
- Druid monitoring statistics source
- So easy 将程序部署到服务器
- Subnet division and subnet summary
- 访问url 404 的错误
- 调试定位导航遇到的问题总结
- Chapitre 03 Bar _ Gestion des utilisateurs et des droits
- 【EXSI】主机间传输文件
猜你喜欢

第03章_用戶與權限管理

C#实现图的深度优先遍历--非递归代码

最新接口自动化面试题

世界上最好的学习法:费曼学习法

E15 solution for cx5120 controlling Huichuan is620n servo error

How do spark tasks of 10W workers run? (Distributed Computing)

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
![Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]](/img/83/e3c9d8eda9d5351d4c54928d3b090b.png)
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
Common interview questions for performance test

Saving images of different depths in opencv
随机推荐
Force buckle - sum of two numbers
Redis tutorial
Lavaweb [first understanding the solution of subsequent problems]
LeetCode_栈_困难_227.基本计算器(不含乘除)
【Qt】添加第三方库的知识补充
Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
Huawei operator level router configuration example | BGP VPLS configuration example
[linear DP] longest common subsequence
Introduction and basic knowledge of machine learning
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
About the application of MySQL
HTB-Lame
So easy deploy program to server
[applet project development -- Jingdong Mall] classified navigation area of uni app
EtherCAT简介
Ctfshow blasting WP
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
How to determine the progress bar loaded in the loading interface when opening the game
通信协议——分类及其特征介绍
[us match preparation] complete introduction to word editing formula