当前位置:网站首页>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边栏推荐
- EtherCAT原理概述
- Restcloud ETL WebService data synchronization to local
- Let's just say I can use thousands of expression packs
- 通信协议——分类及其特征介绍
- HTB-Lame
- 最好用的信任关系自动化脚本(shell)
- C语言多线程编程入门学习笔记
- Lavaweb [first understanding the solution of subsequent problems]
- How do I hide div on Google maps- How to float a div over Google Maps?
- MCU firmware packaging Script Software
猜你喜欢

PHP batch Excel to word
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

HTB-Lame

EtherCAT简介
![[applet project development -- JD mall] uni app commodity classification page (Part 2)](/img/f3/752f41f5b5cc16c8a71498ea9cabb5.png)
[applet project development -- JD mall] uni app commodity classification page (Part 2)

EDLines: A real-time line segment detector with a false detection control翻译

How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet

C#实现基于广度优先BFS求解无权图最短路径----完整程序展示

Redis tutorial

【机器学习】向量化计算 -- 机器学习路上必经路
随机推荐
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Analyze datahub, a new generation metadata platform of 4.7K star
几行事务代码,让我赔了16万
Scale SVG to container without mask / crop
世界上最好的学习法:费曼学习法
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
[us match preparation] complete introduction to word editing formula
Restcloud ETL practice to realize incremental data synchronization without identification bit
Basic concept and classification of sorting
php批量excel转word
LeetCode_栈_困难_227.基本计算器(不含乘除)
Dart training and sphygmomanometer inflation pump power control DPC
Restcloud ETL practice data row column conversion
JS日常开发小技巧(持续更新)
Restcloud ETL data realizes incremental data synchronization through timestamp
Mysql知识点
Subnet division (10)
[linear DP] shortest editing distance
Big orange crazy blog move notice