执行脚本出现error parsing getopt parameters错误,请求排查代码问题
Hey Firas, sorry you hit this roadblock with your script! Since you haven't shared the full code of myscript.sh, I can break down the most common reasons this getopt parsing error happens and what you’re likely missing:
Missing or incorrect long option definitions in getopt
If your script uses long options like--certtype,--password, and--commonname, you need to explicitly declare them in thegetoptcall using the--longflag. Each option that requires a value (like all yours do) should end with a colon:. For example, yourgetoptline should look something like this:OPTIONS=$(getopt -o "" --long certtype:,password:,commonname: -n "$0" -- "$@")If any of these options are missing from the
--longlist, or you forgot the colon for options that take arguments,getoptwill throw a parsing error.Skipping the
eval set --step
After runninggetopt, you need to reassign the parsed output back to your script’s positional parameters. This step ensures your subsequent option-parsing logic can read the parameters correctly. You’ll need a line like:eval set -- "$OPTIONS"Without this, your script won’t properly interpret the parsed options, leading to errors.
Unquoted variables in the getopt call
Always wrap"$@"in double quotes when passing arguments togetopt—this preserves spaces and special characters in your input values. If you wrotegetopt ... -- $@instead ofgetopt ... -- "$@", that could break parameter parsing.
Example Working Getopt Snippet
Here’s a quick example of how this should look in your script to handle your specified options:
#!/bin/bash # Parse command line options OPTIONS=$(getopt -o "" --long certtype:,password:,commonname: -n "$0" -- "$@") if [ $? -ne 0 ]; then echo "Error parsing parameters!" >&2 exit 1 fi # Reset positional parameters to parsed options eval set -- "$OPTIONS" # Process each option while true; do case "$1" in --certtype) CERT_TYPE="$2" shift 2 ;; --password) PASSWORD="$2" shift 2 ;; --commonname) COMMON_NAME="$2" shift 2 ;; --) shift break ;; *) echo "Unknown option: $1" >&2 exit 1 ;; esac done # Rest of your script logic here...
Double-check your script against these points—chances are one of these pieces is missing. If you can share more of your script code, I can give an even more targeted fix!
内容的提问来源于stack exchange,提问作者F. K.

