getopts呼叫功能不要给出任何结果

发布于 2025-01-19 17:47:32 字数 710 浏览 2 评论 0原文

你好,我试图从学校做一个项目,但我有一个小问题,当我尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2025-01-26 17:47:32

getopts解析简短选项,这是一个dash(“ - ”)和a
字母或数字。简短选项的示例是-2,-d和-d。它可以
还可以解析组合的简短选项,例如-2dd。

将n1和n2更改为一个字母x,y。

如果该选项期望参数,getopts获取该参数,
并将其放入$ optarg。如果找不到预期的论点,
变量选择名称设置为结肠(“:”)。 getopts然后增加
位置索引,$ optind,这表明下一个选项是
处理。

删除“:” c之后。 - >而getopts“ x:y:ch” o;做 ...
C不会期望有参数:

usage(){
        echo -e "Example Usage:"
        echo -e "myscript.sh [-h] [-x] [-y] [-c]"
        exit 0
    }

calculator(){
    echo "$num1*$num2"
    mul=$(( $num1*$num2 ))
    echo Result $mul
    exit 0
}

while getopts "x:y:ch" o; do
    case "${o}" in
        x)
            num1=${OPTARG}
            ;;
        y)
            num2=${OPTARG}
            ;;
        c)
            calculator
            ;;
        h)
            usage
            ;;
        *)
            #usage
            ;;
    esac
done


./myscript.sh -x 3 -y 4 -c
3*4
Result 12

如果您不使用长期选项,则

optspec="x:y:ch-:"
while getopts "$optspec" optchar; do
    case "${optchar}" in
        -)
            case "${OPTARG}" in
                n1) num1="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ));;
                n2) num2="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ));;
                n1=*) num1=${OPTARG#*=};;
                n2=*) num2=${OPTARG#*=};;
                *)
                    if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
                       usage
                    fi
                    ;;
            esac;;
        h) usage;;
        c) calculator;;
        x) num1=${OPTARG};;
        y) num2=${OPTARG};;
        *)
            if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
    esac
done



./myscript.sh --n1 3 -n2 4 -c
 or 
 ./myscript.sh --n1=3 -n2=4 -c
 3*4
 Result 12

getopts parses short options, which are a single dash ("-") and a
letter or digit. Examples of short options are -2, -d, and -D. It can
also parse short options in combination, for instance -2dD.

Change n1 and n2 to one letter x, y.

If the option is expecting an argument, getopts gets that argument,
and places it in $OPTARG. If an expected argument is not found, the
variable optname is set to a colon (":"). Getopts then increments the
positional index, $OPTIND, that indicates the next option to be
processed.

remove ":" after c. --> while getopts "x:y:ch" o; do ...
c is not expecting an argument

usage(){
        echo -e "Example Usage:"
        echo -e "myscript.sh [-h] [-x] [-y] [-c]"
        exit 0
    }

calculator(){
    echo "$num1*$num2"
    mul=$(( $num1*$num2 ))
    echo Result $mul
    exit 0
}

while getopts "x:y:ch" o; do
    case "${o}" in
        x)
            num1=${OPTARG}
            ;;
        y)
            num2=${OPTARG}
            ;;
        c)
            calculator
            ;;
        h)
            usage
            ;;
        *)
            #usage
            ;;
    esac
done


./myscript.sh -x 3 -y 4 -c
3*4
Result 12

If you wan't use long options:

optspec="x:y:ch-:"
while getopts "$optspec" optchar; do
    case "${optchar}" in
        -)
            case "${OPTARG}" in
                n1) num1="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ));;
                n2) num2="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ));;
                n1=*) num1=${OPTARG#*=};;
                n2=*) num2=${OPTARG#*=};;
                *)
                    if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
                       usage
                    fi
                    ;;
            esac;;
        h) usage;;
        c) calculator;;
        x) num1=${OPTARG};;
        y) num2=${OPTARG};;
        *)
            if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
    esac
done



./myscript.sh --n1 3 -n2 4 -c
 or 
 ./myscript.sh --n1=3 -n2=4 -c
 3*4
 Result 12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文