当前位置:网站首页>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边栏推荐
- JS日常开发小技巧(持续更新)
- Mybati SQL statement printing
- Metadata in NFT
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- 倍福TwinCAT3 Ads相关错误详细列表
- 最新接口自动化面试题
- Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
- Multithreaded printing
- So easy deploy program to server
- C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
猜你喜欢
随机推荐
How to verify whether the contents of two files are the same
STM32——一线协议之DS18B20温度采样
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
C language EXECL function
E15 solution for cx5120 controlling Huichuan is620n servo error
Dart training and sphygmomanometer inflation pump power control DPC
通信协议——分类及其特征介绍
Mysql知识点
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
串口接收数据方案设计
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
Magnetic manometer and measurement of foreign coins
Redis分布式锁的8大坑
【日常训练】1175. 质数排列
最好用的信任关系自动化脚本(shell)
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
家居网购项目
Lavaweb [first understanding the solution of subsequent problems]
Poj-3486-computers[dynamic planning]




![[linear DP] shortest editing distance](/img/2f/9a6f661bf478fdd5d53e5a03d7297d.jpg)



