当前位置:网站首页>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 ]
边栏推荐
- Minimum number of steps to get out of the maze 2
- PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
- Docker interview question 2--get the number of database connections and docker-compose
- OSS-访问oss生成的url无法访问,直接下载问题
- 【报错】ModuleNotFoundError: No module named ‘paramiko‘
- Pagoda measurement - building LightPicture open source map bed system
- How to turn off system protection in Win11?How to turn off the system protection restore function?
- 基于FPGA的任意字节数的串口接收(含源码工程)
- 宽带由20M换为100M
- Docker 面试题2则--取数据库连接数和docker-compose
猜你喜欢
关于HBuilder X鼠标变为_ 替换字符的问题
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
RedHat红帽RHEL7安装与使用,VMware Workstation16 Pro虚拟机的安装与使用
c语言指针练习题
Leetcode80. 删除有序数组中的重复项 II
ITK编译remote库
Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
随机推荐
CMake 编译运行dpdk项目程序
DALL·E-2是如何工作的以及部署自己的DALL·E模型
微信公众号如何开通支付功能?
-向量点积-
Solidity最强对手:MOVE语言及新公链崛起
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
DHCP——动态主机配置协议
【Django】缓存
嵌入式Qt-实现两个窗口的切换
Win7怎么把控制面板添加到右键菜单
Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid
XSS详解及复现gallerycms字符长度限制短域名绕过
-red and black-
@PostConsturct注解作用及特点
这一次,话筒给你:向自由软件之父 Richard M. Stallman 提问啦!
移动终端数据业务高安全通信方案研究
【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格
RedHat红帽RHEL7安装与使用,VMware Workstation16 Pro虚拟机的安装与使用
快速响应性智能型/智能响应性聚乙二醇纳米/还原响应型水凝胶的研究与制备
【软考软件评测师】软件测试基础知识