当前位置:网站首页>The shell script uses two bars to receive external parameters
The shell script uses two bars to receive external parameters
2022-07-01 03:19:00 【Saggitarxm】
First , The effect is this :
Can handle both short options (-) You can also handle long options (--)
[[email protected] shell]$ ./demo.sh --help
sqoop The program starts running : demo.sh
Usage: ./demo.sh [options]
Options:
--append, -a: Append import ( The default is append mode )
--overwrite, -o: Overlay import
--method, -m: single- One day import
interval- Interval import
all- Full table import
--date, -d: One day import , Some date data ( The format is yyyymmdd)
--startdate, -s: Interval import , Start date
--enddate, -e: Interval import , End date
--help, -h help
shell There is a very simple way for scripts to take external parameters , Use... In scripts $0,$1,$2... Refers to the first parameter passed in when executing a script ($0 It's the script name ).
however , It's not elegant after all ,
Another way shell Use... In scripts getopts command , Only short options are allowed (eg:-d,-s,-h), Very convenient , Relatively simple , You can search for it yourself .
But if you want to achieve the above effect, support both long options and short options (eg:--date,-d,--startdate,-s,--help,-h),
You can only use getopt The command :
# Define Command Execution Options
# Parameters are not followed by a colon (a) Indicates that no parameter is followed eg:-a,--append
# The parameter is followed by a colon (a:) It means that parameters must be followed eg:-d 20201224,--date 20201224
# Parameters are followed by two colons (a::) Indicates whether the following parameters can be connected or not
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: Append import ( The default is append mode )\n --overwrite, -o: Overlay import \n\n --method, -m: single- One day import \n interval- Interval import \n all- Full table import \n\n --date, -d: One day import , Some date data ( The format is yyyymmdd)\n\n --startdate, -s: Interval import , Start date \n --enddate, -e: Interval import , End date \n\n --help, -h help "
exit 1
fi
# Assign normalized command line parameters to positional parameters ($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}"
# Accept Execution Options ; Assign a value to a variable
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: Append import ( The default is append mode )\n --overwrite, -o: Overlay import \n\n --method, -m: single- One day import \n interval- Interval import \n all- Full table import \n\n --date, -d: One day import , Some date data ( The format is yyyymmdd)\n\n --startdate, -s: Interval import , Start date \n --enddate, -e: Interval import , End date \n\n --help, -h help "
exit 0
;;
?)
echo "missing options, pls check!"
exit 1
;;
esac
done边栏推荐
- md5sum操作
- EtherCAT原理概述
- 实战 ELK 优雅管理服务器日志
- Redis efficient like and cancel function
- 限流组件设计实战
- 倍福TwinCAT3 Ads相关错误详细列表
- Summary of problems encountered in debugging positioning and navigation
- Error accessing URL 404
- [us match preparation] complete introduction to word editing formula
- Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
猜你喜欢

Hal library setting STM32 interrupt

Let's just say I can use thousands of expression packs

伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示

Druid monitoring statistics source

数据交换 JSON
![Servlet [first introduction]](/img/2a/aff3b93e43550d30a33c1683210d3a.png)
Servlet [first introduction]

Hello World generation

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

Metadata in NFT

POI导出excel,按照父子节点进行分级显示
随机推荐
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
实战 ELK 优雅管理服务器日志
Best used trust automation script (shell)
手把手带你了解一块电路板,从设计到制作(干货)
监听器 Listener
【机器学习】向量化计算 -- 机器学习路上必经路
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
Mysql知识点
leetcode 1818 绝对值,排序,二分法,最大值
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
【Qt】添加第三方库的知识补充
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
ctfshow爆破wp
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
Catch 222222
[linear DP] shortest editing distance
C#实现图的深度优先遍历--非递归代码
POI导出excel,按照父子节点进行分级显示
倍福TwinCAT3 Ads相关错误详细列表
Redis efficient like and cancel function