当前位置:网站首页>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 ]
边栏推荐
猜你喜欢
随机推荐
Shader Graph学习各种特效案例
-采花生-
解决sed替换文本,里面含有“/“、“#”等特殊字符的问题
How to activate the payment function on WeChat official account?
手把手教你编写性能测试用例
小程序中计算距离信息
Problems and solutions related to Chinese character set in file operations in ABAP
PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
UI遍历的初步尝试
初步认识对象
【ROS2原理10】Interface数据的规定
【LeetCode】求根节点到叶节点数字之和
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
Solidity最强对手:MOVE语言及新公链崛起
Web性能测试模型小结
-Chess game-
高校就业管理系统设计与实现
Docker 面试题2则--取数据库连接数和docker-compose
C language structure, function and pointer exercise (simple address book)
MySQL最大连接数限制如何修改









