bash getopts 循环不迭代

bash getopts loop not iterating

我在很多 bash 脚本中都使用了一个简单的循环,但这个特定的循环似乎不起作用。

#!/bin/bash
function main
{
    echo here
    while getopts "Ah" cli_opt; do
      case ${cli_opt} in
        A)
            echo "op A"
            start_all
            return $?
        ;;
        *)
            echo invalid option
            showHep
            exit 1
        ;;
        h)
            showHelp
            exit 0
        ;;
        \?)
            invalid option
            showHelp
            exit 1
        ;;
        :)
            option -$OPTARG requires an argument
            showHelp
            exit 1
         ;;
      esac
    done
}

main
exit 0

无论我怎么称呼它,它只达到"here"。

修复、更改:

main

至:

main "$@"

"$@" 是一个包含所有参数的数组。您需要将此数组传递给 main 函数。