使用 for 循环解析参数
一个提供选项的简单示例:
| 选择 | Alt 键。选择 | 细节 | 
|---|---|---|
| -h | --help | 显示帮助 | 
| -v | --version | 显示版本信息 | 
| -dr path | --doc-root path | 带有辅助参数(路径)的选项 | 
| -i | --install | 布尔选项(true / false) | 
| -* | - | 选项无效 | 
#!/bin/bash
dr=''
install=false
skip=false
for op in "$@";do
    if $skip;then skip=false;continue;fi
    case "$op" in
        -v|--version)
            echo "$ver_info"
            shift
            exit 0
            ;;
        -h|--help)
            echo "$help"
            shift
            exit 0
            ;;
        -dr|--doc-root)
            shift
            if [[ "$1" != "" ]]; then
                dr="${1/%\//}"
                shift
                skip=true
            else
                echo "E: Arg missing for -dr option"
                exit 1
            fi
            ;;
        -i|--install)
            install=true
            shift
            ;;
        -*)
            echo "E: Invalid option: $1"
            shift
            exit 1
            ;;
    esac
done