当前位置:网站首页>shell指定参数名传参
shell指定参数名传参
2022-08-10 00:33:00 【杨林伟】
如果使用shell指定参数名称传参,是使用到了“getopts”命令,下面举个例子,定义入参(-n NAME and -t TIMES.)并获取参数,里面已包含注释:
#!/bin/bash
# 1. 定义参数变量名
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. while循环设置进定义的变量名,并校验参数的合法性
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. 使用定义的变量名
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 ]
边栏推荐
- 递归 二分查找 冒泡排序 快速排序
- Mysql database ALTER basic operations
- 改变社交与工作状态的即时通讯是什么呢?
- Redisson 分布式锁
- Pyscript,创建一个能执行crud操作的网页应用
- R语言使用glm函数构建逻辑回归模型(logistic)、使用subgroupAnalysis函数进行亚组分析并可视化森林图
- 3.1 - 程序设计语言 3.2 - 高级语言的特点及引用 3.3 - 静态/动态类型语言
- 温度响应性纳米水凝胶光子品体/纤维素修饰荧光水凝胶/载脂质体水凝胶的制备方法
- PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明
- -Chess game-
猜你喜欢

pytest:如何在测试中编写和报告断言

y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)

渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)

Pyscript,创建一个能执行crud操作的网页应用

有PEG-Biotin参与的(CAS:1778736-18-7)Biotin-PEG4-OH广泛用于分子靶点检测

Web性能测试模型小结

CAS:851113-28-5 (生物素-ahx-ahx-酪胺)

【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI

D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description

惊掉你下巴,程序员编码竟然可以被 996 指数化
随机推荐
Solidity最强对手:MOVE语言及新公链崛起
C language structure, function and pointer exercise (simple address book)
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
How to add control panel to right click menu in win7
Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
微信小程序tab切换时保存checkbox状态
Docker 面试题2则--取数据库连接数和docker-compose
《痞子衡嵌入式半月刊》 第 60 期
防勒索病毒现状分析
Mysql database ALTER basic operations
PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
数据建模已死,真的吗?
XSS详解及复现gallerycms字符长度限制短域名绕过
C语言头文件组织与包含原则
使用 apxs 构建和安装 Apache 扩展共享对象模块
【无标题】
Koa中间件next实现