If a required argument is not found, and getopts is not silent, a question mark (?) is placed inname,OPTARGis unset, and a diagnostic message is printed. If getopts is silent, then a colon (:) is placed innameandOPTARGis set to the option character found. 注意:getopts是 bash 的内置...
echo"Invalid option: -$OPTARG";usage;;# 当传入未定义选项时, getopts会返回 ?, \? 为转义字符:)echo"Option -$OPTARGrequires an argument.";usage;;# 当需要参数的选项没有传入参数时, 会提示该错误esacdoneecho$a$b$c# 比如 bash bash_tutorial.sh -a 1 -b 2 -c yes -d 1, 将输出: Invalid...
/bin/bashERR_MSG="You did not give the argument required"if[[${1?$ERR_MSG}=="a"]];thenecho"ABC"elif[[${1?$ERR_MSG}=="b"]];thenecho"123"elif[[${1?$ERR_MSG}=="c"]];thenecho".*?"elseecho"You did not provide the argument correctly"exit1fi Run Code Online (Sandbox Code...
如果以后的决策有必要,我还会设置一个标志来指示用户明确设置该值,但根据我的经验,这通常不需要。 slurm_template_file='submit_gaussian_template.slurm'slurm_template_file_isset=falsewhilegetopts't:'optdocase"$opt"in(t) slurm_template_file="$OPTARG"slurm_template_file_isset=true;;esacdoneif"$slurm_t...
Is ESAC a required delimiter in Bash? Does the first script work with Bash menu based case ESAC? What is exec command in Bash? How to Avoid Argument Conflicts in a Bash Script? Question: In my bash script, I'm aiming to verify that one of the arguments provided is a legitimate group...
运行示例:
*)set"unknown""$1";break;;*) REST="${REST}\"\${$(($OPTIND-$#))}\""esacshiftdone[$#-eq0 ]&&{ OPTIND=1;unsetOPTARG;return0;}case$1inunknown)set"Unrecognized option:$2""$@";; noarg)set"Does not allow an argument:$2""$@";; required)set"Requires an argument:$2""$@";...
while getopts ":h" option; do case $option in h) # display Help Help exit;; esac done echo "Hello world!" Notice the double semicolon at the end of the exit statement in the case option for-h. This is required for each option. Add to this case statement to delineate the end of...
$ checkarg /home/chris/bin/checkarg: line 10: 1: An argument is required $ checkarg x /home/chris/bin/checkarg: line 10: 2: Two arguments are required $ checkarg '' '' /home/chris/bin/checkarg: line 13: 1: A non-empty argument is required $ checkarg x '' /home/chris/bin...
getopts places ? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and no diagnostic message is printed. If a required argument is not found, and getopts...