getopts呼叫功能不要给出任何结果
你好,我试图从学校做一个项目,但我有一个小问题,当我尝试通过 getopts 执行该函数时,我没有得到任何结果,我现在陷入困境。
我在 getopts 中调用函数的方式是否有问题,因为如果我尝试使用 -h 显示帮助菜单效果很好,但尝试使用 -c 进行乘法不会给出任何结果
usage(){
echo -e "Example Usage:"
echo -e "myscript.sh [-h] [-n1] [-n2] [-c]"
exit 0
}
calculator(){
echo "$num1*$num2"
mul=$(( $num1*$num2 ))
echo Result $mul
exit 0
}
while getopts ":n1:n2:c:h" o; do
case "${o}" in
n1)
num1=${OPTARG}
;;
n2)
num2=${OPTARG}
;;
c)
calculator
;;
h)
usage
;;
*)
#usage
;;
esac
done
Hello i trying to do a project from school, but i have a little problem, when i try to execute the function via getopts i don't get any result, im stuck right now.
Is there something wrong about how im calling a function in getopts, because if i try to display the help menu with -h works well, but trying to make the multiplication with -c don't give any result
usage(){
echo -e "Example Usage:"
echo -e "myscript.sh [-h] [-n1] [-n2] [-c]"
exit 0
}
calculator(){
echo "$num1*$num2"
mul=$(( $num1*$num2 ))
echo Result $mul
exit 0
}
while getopts ":n1:n2:c:h" o; do
case "${o}" in
n1)
num1=${OPTARG}
;;
n2)
num2=${OPTARG}
;;
c)
calculator
;;
h)
usage
;;
*)
#usage
;;
esac
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将n1和n2更改为一个字母x,y。
删除“:” c之后。 - >而getopts“ x:y:ch” o;做 ...
C不会期望有参数:
如果您不使用长期选项,则
Change n1 and n2 to one letter x, y.
remove ":" after c. --> while getopts "x:y:ch" o; do ...
c is not expecting an argument
If you wan't use long options: