当前位置:网站首页>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 ]
边栏推荐
- -向量点积-
- [LeetCode] Find the sum of the numbers from the root node to the leaf node
- 微信小程序tab切换时保存checkbox状态
- 芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
- 365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy
- 开发IM即时通讯容易吗?需要什么技术
- R语言使用coxph函数构建生存分析回归模型,使用forestmodel包的forest_model函数可视化生存回归模型对应的森林图
- 删除表空间数据文件
- DP 优化方法合集
- Unity image使用长图后 图片很糊
猜你喜欢
GBJ1510-ASEMI机器人电源整流桥GBJ1510
初步认识对象
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
Pyscript,创建一个能执行crud操作的网页应用
Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid
Win7怎么把控制面板添加到右键菜单
DALL·E-2是如何工作的以及部署自己的DALL·E模型
C language structure, function and pointer exercise (simple address book)
基于设计稿识别的可视化低代码系统实践
使用 GoogleTest 框架对 C 代码进行单元测试
随机推荐
Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid
Web性能测试模型小结
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
How to add control panel to right click menu in win7
Solidity最强对手:MOVE语言及新公链崛起
宝塔实测-搭建LightPicture开源图床系统
Qt的pro文件递归搜寻添加文件
【报错】ModuleNotFoundError: No module named ‘paramiko‘
Prometeus 2.31.0 新特性
商业模式及其 SubDAO 深入研究
XSS详解及复现gallerycms字符长度限制短域名绕过
eyb:Redis学习(4)
Summary of Web Performance Testing Models
不是吧,连公司里的卷王写代码都复制粘贴,这合理?
Data storage - the C language
[LeetCode] Find the sum of the numbers from the root node to the leaf node
CMake 编译运行dpdk项目程序
How to turn off system protection in Win11?How to turn off the system protection restore function?
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
防勒索病毒现状分析