当前位置:网站首页>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边栏推荐
- Hal library operation STM32 serial port
- A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
- Latest interface automation interview questions
- Classic programming problem: finding the number of daffodils
- 串口接收数据方案设计
- Poj-3486-computers[dynamic planning]
- Subnet division (10)
- XXL job User Guide
- C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
- [applet project development -- JD mall] uni app commodity classification page (first)
猜你喜欢

xxl-job使用指南

Introduction and basic knowledge of machine learning

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

Detailed explanation of pointer array and array pointer (comprehensive knowledge points)

Hal library operation STM32 serial port
性能测试常见面试题

STM32 - DS18B20 temperature sampling of first-line protocol
![[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)](/img/73/a22ab1dbb46e743ffd5f78b40e66a2.png)
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)

Analyze datahub, a new generation metadata platform of 4.7K star

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
随机推荐
Basic concepts of database
JS日常开发小技巧(持续更新)
leetcode 1818 绝对值,排序,二分法,最大值
ES6解构语法详解
数据交换 JSON
Error accessing URL 404
EDLines: A real-time line segment detector with a false detection control翻译
ctfshow爆破wp
一文讲解发布者订阅者模式与观察者模式
调试定位导航遇到的问题总结
Chapter 03_ User and authority management
几行事务代码,让我赔了16万
家居网购项目
Introduction and basic knowledge of machine learning
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
Force buckle - sum of two numbers
倍福TwinCAT3 Ads相关错误详细列表
So easy 将程序部署到服务器
Druid monitoring statistics source
Introduction to ieda right click source file menu