当前位置:网站首页>Introduction to the usage of getopts in shell
Introduction to the usage of getopts in shell
2022-07-03 08:53:00 【Xiaoping is extraordinary】
One 、getopts Command profile
getopts Command is used to parse Shell Tools for scripting command line parameters ,getopts Command parameters contain option characters that need to be recognized , If the option character is followed by a colon , Indicates that the character option requires a command line parameter ( The option characters and the corresponding command line parameters are separated by spaces )( Be careful : The colon & Question marks cannot be used as option characters ).getopts Every time a command is called , Put it in the next variable ,OPTARG Then you can get the parameter value ; If option Put a colon in front of it , It means ignoring the error
1.1 Command use format
getopts optstring name [arg...]
- optstring List the corresponding Shell All parameters recognized by the script , for example : Need to use -a,-f,-s When parameters are ,optstring yes afs; If necessary, the command line parameter is followed by a value , In the corresponding optstring Add a colon to the back , for example a:fs Express a There will be a value after the command line parameter , yes **-a value** In the form of ;
- getopts If... Is matched during execution a Parameters , Will be able to -a Parameter corresponding value Stored in a place called OPTARG Of Shell Variable in ;
- If optstring It starts with a colon , It indicates that when the command line appears optstring No error message will be prompted for parameters not in
Two 、 Example
stay Shell Script , For simple parameters , Often use $1,2 , . . . , n Just deal with it . But when there are too many command line parameters or we need to distinguish the use of command line parameters , You need to use getopts command
2.1
#!/bin/bash
func() {
echo "Usage:"
echo "test.sh [-j S_DIR] [-m D_DIR]"
echo "Description:"
echo "S_DIR,the path of source."
echo "D_DIR,the path of destination."
exit -1
}
upload="false"
while getopts 'h:j:m:u' OPT; do
case $OPT in
j) S_DIR="$OPTARG";;
m) D_DIR="$OPTARG";;
u) upload="true";;
h) func;;
?) func;;
esac
done
echo $S_DIR
echo $D_DIR
echo $upload
situation 1:
sh test.sh -j /data/usw/web -m /opt/data/web
## Output results
/data/usw/web
/opt/data/web
false
situation 2:
getopts In the option character of the command , If you don't follow : The letter of is Switch type options , You do not need to specify a value , Equate to true/false, As long as you take this parameter with you, it's true. In the following example -u
getopts After the command recognizes each option , You can cooperate with case To operate . In operation , There are two " Constant ", One is OPTARG, To get the value of the current option ; The other is OPTIND, Express The displacement of the current option in the parameter list .case Of The last one is ?, Is used to identify illegal options , And carry out the corresponding operation , Our script outputs help information
sh test.sh -j /data/usw/web -m /opt/data/web -u
## Output results
/data/usw/web
/opt/data/web
true
situation 3:
sh test.sh -h
## Output results
test.sh: option requires an argument -- h
Usage:
test.sh [-j S_DIR] [-m D_DIR]
Description:
S_DIR,the path of source.
D_DIR,the path of destination.
situation 4:
sh test.sh j
## Output results
false
2.2
#!/bin/bash
func() {
echo "func:"
echo "test.sh [-j S_DIR] [-m D_DIR]"
echo "Description:"
echo "S_DIR, the path of source."
echo "D_DIR, the path of destination."
exit -1
}
upload="false"
echo $OPTIND
while getopts 'j:m:u' OPT; do
case $OPT in
j) S_DIR="$OPTARG";;
m) D_DIR="$OPTARG";;
u) upload="true";;
?) func;;
esac
done
echo $OPTIND
shift $(($OPTIND - 1))
echo $1
situation 1:
sh test.sh -j /data/usw/web beijing
## Output results
1
3
beijing
situation 2:
sh test.sh -m /opt/data/web beijing
1
3
beijing
situation 3:
sh test.sh -j /data/usw/web -m /opt/data/web beijing
1
5
beijing
situation 4:
sh test.sh -j /data/usw/web -m /opt/data/web -u beijing
1
6
beijing
边栏推荐
- 注解简化配置与启动时加载
- Binary to decimal, decimal to binary
- PHP function date (), y-m-d h:i:s in English case
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- Get the link behind? Parameter value after question mark
- Slice and index of array with data type
- 樹形DP AcWing 285. 沒有上司的舞會
- producer consumer problem
- Find the combination number acwing 886 Find the combination number II
- [concurrent programming] explicit lock and AQS
猜你喜欢

记忆化搜索 AcWing 901. 滑雪

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

Redux - learning notes

Phpstudy 80 port occupied W10 system
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

ES6 promise learning notes

TP5 multi condition sorting

我们有个共同的名字,XX工
随机推荐
[concurrent programming] explicit lock and AQS
cres
【Rust 笔记】11-实用特型
producer consumer problem
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
单调栈-84. 柱状图中最大的矩形
Phpstudy 80 port occupied W10 system
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
Binary tree sorting (C language, char type)
Unity Editor Extension - drag and drop
【Rust笔记】06-包和模块
Monotonic stack -84 The largest rectangle in the histogram
Es8 async and await learning notes
树形DP AcWing 285. 没有上司的舞会
22-05-26 西安 面试题(01)准备
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
LeetCode 241. 为运算表达式设计优先级
Unity interactive water ripple post-treatment
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
PHP uses foreach to get a value in a two-dimensional associative array (with instances)