当前位置:网站首页>The shell specifies the parameter name to pass the parameter
The shell specifies the parameter name to pass the parameter
2022-08-10 02:13:00 【Yang Linwei】
如果使用shellSpecify the parameter name to pass the parameter,是使用到了“getopts”命令,下面举个例子,定义入参(-n NAME and -t TIMES.)并获取参数,Contains comments:
#!/bin/bash
# 1. Define the parameter variable name
NAME="" # Name of person to greet.
TIMES=1 # Number of greetings to give.
# 2. 用法提示
usage() {
# Function: Print a help message.
echo "Usage: $0 [ -n NAME ] [ -t TIMES ]" 1>&2
}
exit_abnormal() {
# Function: Exit with error.
usage
exit 1
}
# 3. whileThe loop sets the variable name into the definition,And verify the validity of the parameters
while getopts ":n:t:" options; do # Loop: Get the next option;
# use silent error checking;
# options n and t take arguments.
case "${options}" in #
n) # If the option is n,
NAME=${OPTARG} # set $NAME to specified value.
;;
t) # If the option is t,
TIMES=${OPTARG} # Set $TIMES to specified value.
re_isanum='^[0-9]+$' # Regex: match whole numbers only
if ! [[ $TIMES =~ $re_isanum ]] ; then # if $TIMES not whole:
echo "Error: TIMES must be a positive, whole number."
exit_abnormal
exit 1
elif [ $TIMES -eq "0" ]; then # If it's zero:
echo "Error: TIMES must be greater than zero."
exit_abnormal # Exit abnormally.
fi
;;
:) # If expected argument omitted:
echo "Error: -${OPTARG} requires an argument."
exit_abnormal # Exit abnormally.
;;
*) # If unknown (any other) option:
exit_abnormal # Exit abnormally.
;;
esac
done
# 4. Use the defined variable name
if [ "$NAME" = "" ]; then # If $NAME is an empty string,
STRING="Hi!" # our greeting is just "Hi!"
else # Otherwise,
STRING="Hi, $NAME!" # it is "Hi, (name)!"
fi
COUNT=1 # A counter.
while [ $COUNT -le $TIMES ]; do # While counter is less than
# or equal to $TIMES,
echo $STRING # print a greeting,
let COUNT+=1 # then increment the counter.
done
exit 0
运行结果:
./greeting
Hi!
-----
./greeting -n Dave
Hi, Dave!
-----
./greeting -t 3
Hi!
Hi!
Hi!
-----
./greeting -t 4 -n Betty
Hi, Betty!
Hi, Betty!
Hi, Betty!
Hi, Betty!
------
./greeting -n
Error: -n requires an argument.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
------
./greeting -t
Error: -t requires an argument.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
------
./greeting -t 0
Error: TIMES must be greater than zero.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
边栏推荐
- ITK编译remote库
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 基于Web的疫情隔离区订餐系统
- 基于SSM实现手机销售商城系统
- UI遍历的初步尝试
- 365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy
- Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
- 开发IM即时通讯容易吗?需要什么技术
- 数据建模已死,真的吗?
- 不是吧,连公司里的卷王写代码都复制粘贴,这合理?
猜你喜欢

unity编辑器扩展界面使用 List

基于Web的疫情隔离区订餐系统

DHCP——动态主机配置协议

万字总结:分布式系统的38个知识点

CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)

OpenSSF的开源软件风险评估工具:Scorecards

What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?

【无标题】

XSS高级 svg 复现一个循环问题以及两个循环问题

Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation
随机推荐
破产企业的职工退休怎么办?
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
Aptos 深度解读:机遇、挑战与风险
Solve the problem of sed replacement text containing special characters such as "/" and "#"
Characteristics of the (CAS:1527486-16-3TAMRA-azide-PEG3-Biotin) reaction in biotin azide!
[LeetCode] Find the sum of the numbers from the root node to the leaf node
Not, even the volume of the king to write code in the company are copying and pasting it reasonable?
SonarQube升级记录:7.8->7.9->8.9
ABAP 里文件操作涉及到中文字符集的问题和解决方案
Unity image使用长图后 图片很糊
3438. 数制转换
-向量点积-
删除表空间数据文件
Pagoda measurement - building LightPicture open source map bed system
小程序中计算距离信息
Shader Graph learns various special effects cases
OSS-访问oss生成的url无法访问,直接下载问题
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
[obs] obsqsv11 hard coding and comparison with metartc codec
Win7怎么把控制面板添加到右键菜单