shell 的getopt 指令之迷之错误?

目前给脚本写了一个getopt 加入了以下参数

usage() {
    cat <<-EOF
    Usage: sh|bash $sh_name -[option]
    
    sh  $sh_name -e | --env-set                       (Manually set hcs environment variables with higher priority than setting.conf, example: -e DB_HOST=10.77.0.28 or --env-set REDIS_HOST=10.770.029)
    sh  $sh_name -t | --timeout-periods               (Set the timeout of the script, if not set the default value is 120 seconds, you can set the time interval in seconds, minutes, hours, days, for example, ten days is set to 10d, three hours is set to 3h and so on and so forth)
    sh  $sh_name -T | --test-mode                     (Start debug mode, this stage will not actually execute the installation script, but will print some process parameters)
    sh  $sh_name -s | --skip-mode                     (This mode will skip some health checks such as checking ip and network card name, disk space, etc.)
    sh  $sh_name -S | --secure-mode                   (Manually enter some private configuration such as mysql password, redis password, etc.)
    sh  $sh_name -r | --relaxed-mode                  (Loose mode : no assignment command will be executed: the hcs installation media will be manually assigned, and the alias function of docker-compose will not be generated in /etc/profile)
    sh  $sh_name -v | --version                       (Print hcs version information)
    sh  $sh_name -h | --help                          (usages for help)
EOF
}

假设脚本为 billity.sh

目前执行 一下指令皆无报错

sh billity.sh -e key=value
sh billity.sh -h
sh billity.sh -T
sh billity.sh -t
sh billity.sh -t100s

如果有多个指令就报错 譬如

sh billity.sh -T -e key=value

image.png

如果把两个参数放在一起 又可以了

sh billity.sh -Te key=value

image.png

而且此时 -e 如果有多个参数 要写成

-e key1 key2 key3这种形式

而不是我想要的 -e key1 -e key2 -e key3

下面贴一下脚本


usage() {
    cat <<-EOF
    Usage: sh|bash $sh_name -[option]
    
    sh  $sh_name -e | --env-set                       (Manually set hcs environment variables with higher priority than setting.conf, example: -e DB_HOST=10.77.0.28 or --env-set REDIS_HOST=10.770.029)
    sh  $sh_name -t | --timeout-periods               (Set the timeout of the script, if not set the default value is 120 seconds, you can set the time interval in seconds, minutes, hours, days, for example, ten days is set to 10d, three hours is set to 3h and so on and so forth)
    sh  $sh_name -T | --test-mode                     (Start debug mode, this stage will not actually execute the installation script, but will print some process parameters)
    sh  $sh_name -s | --skip-mode                     (This mode will skip some health checks such as checking ip and network card name, disk space, etc.)
    sh  $sh_name -S | --secure-mode                   (Manually enter some private configuration such as mysql password, redis password, etc.)
    sh  $sh_name -r | --relaxed-mode                  (Loose mode : no assignment command will be executed: the hcs installation media will be manually assigned, and the alias function of docker-compose will not be generated in /etc/profile)
    sh  $sh_name -v | --version                       (Print hcs version information)
    sh  $sh_name -h | --help                          (usages for help)
EOF
}


OS_ID=$(sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release1 >/dev/null 2>&1) || OS_ID='openEuler'
version_infos="OPENHCS one-click installation and deployment script "1.4.26_3218"
OPENHCS runtime environment (AdoptOpenHCS)(1.4.26_3218)
OPENHCS 64-bit Server Virtual Machine ($OS_ID) (build 25.292-b10, mixed mode)"

getopt -T &> /dev/null
[   $? -ne 4 ] && {
  color_printf "red"   "In view of this system does not support getopt enhancement mode, so this script does not support dynamic option parameters, if you have questions, please contact the administrator wwd"                                                                     1
}


echo ${ppt[@]}
parameters=$(getopt -o e:t::hsrvTS -l env-set:,timeout-periods::,help,skip-mode,relaxed-mode,version,test-mode,secure-mode -n "${sh_name}" -- "${ppt[@]}")
[ $? -ne 0 ] && {
    echo "Try '$sh_name --help' for more information."
    exit 1
} 

eval set -- "$parameters"

while true; do
    case "$1" in
    -e | --env-set)
        eval "$2 >/dev/null 2>&1" 
        shift 2
        ;;
    -t | --timeout-periods)
        echo $1
        echo $2
        case "$2" in
        '')
            shift 2
            break
            ;;
        *)
            if [[ $2 =~ ^[1-9][0-9]*[s|h|m|d]{0,1}$ ]]; then
                TIME_OUT_SECONDS=$2
            else
                :
            fi
            shift 2
            break
            ;;
        esac
        ;;
    -T | --test-mode)
        test_mode=true
        shift
        ;;
    -s | --skip-mode)
        skip_mode=true
        shift
        ;;
    -S | --secure-mode)
      read -sp "Please enter the database hypervisor password: " DB_ROOT_PWD
      echo 
      read -sp "Please enter the redis connection password: " REDIS_PWD
      echo 
        shift
        ;;
    -r | --relaxed-mode)
        relaxed_mode=true
        shift
        ;;
    -v | --version)
        echo "${version_infos}"
        exit
        ;;
    -h | --help)
        usage
        exit
        ;;
    --)
        shift
        break
        ;;
    *)
        usage
        exit
        ;;
    esac
done

求大佬指点 谢谢

阅读 1.9k
1 个回答

好吧 已经找到解决方案了 原因就是

在主脚本里面设置了param 但是没有把他识别为数组导致的

declare -a param=($@)

即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题