Bash getopts第二个标志不是必需的?
存在问题,确保Bash getopts第二个标志不是必需的?,bash,getopts,Bash,Getopts,存在问题,确保-v和-o都是必需的元素,但-h不在我的getopts中。我该如何解决这个问题 usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; } if ( ! getopts ":v:o:h" opt); then echo "Usage: `basename $0` options (-v [version]) (-o [organization unit
-v
和-o
都是必需的元素,但-h
不在我的getopts中。我该如何解决这个问题
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
if ( ! getopts ":v:o:h" opt); then
echo "Usage: `basename $0` options (-v [version]) (-o [organization unit]) -h for help";
exit $E_OPTERROR;
fi
while getopts ":v:o:h" opt; do
case $opt in
v) vermajor=$OPTARG;;
o) org_unit=$OPTARG;;
h) usage;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
esac
done
exit
usage(){echo“usage:$0[-v][-o]”1>&2;退出1;}
如果(!getopts):v:o:h“opt);然后
echo“用法:`basename$0`options(-v[version])(-o[organization unit])-h以获取帮助”;
退出$E_OPTERROR;
fi
而getopts:v:o:h“opt;做
案例$opt-in
v) vermajor=$OPTARG;;
o) 组织单位=$OPTARG;;
h) 用法;;
\?)echo“无效选项:-$OPTARG”>&2;出口1;;
:)echo“选项-$OPTARG需要一个参数。”>&2;出口1;;
以撒
完成
出口
以这个结尾
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
if [[ "$1" =~ "^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$" ]]; then
usage
else
while [[ $# -gt 0 ]]; do
opt="$1"; shift
current_arg="$1"
if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
echo "==> WARNING: You may have left an argument blank. Double check your command."
fi
case "$opt" in
"-v"|"--version" ) vermajor="$1"; shift;;
"-o"|"--org" ) org_unit="$1"; shift;;
* ) echo "==> ERROR: Invalid option: \""$opt"\"" >&2
exit 1;;
esac
done
fi
if [[ "$vermajor" == "" || "$org_unit" == "" ]]; then
echo "ERROR: Options -v and -o require arguments." >&2; exit 1
fi
exit
usage(){echo“usage:$0[-v][-o]”1>&2;退出1;}
如果[“$1”=~”^(({1,2})([Hh]$|[Hh][Ee][Ll][Pp]))$”];然后
使用
其他的
而[$#-gt 0]];做
opt=“$1”;转移
当前参数=“1美元”
如果[[“$current_arg”=~^-{1,2}.*];然后
echo“==>警告:您可能将参数留空。请仔细检查您的命令
fi
案例“$opt”加入
“-v”|“--version”)vermajor=“$1”;移位;;
“-o”|“--org”)组织单位=“$1”;移位;;
*)echo“==>错误:无效选项:\”“$opt\”“>&2
出口1;;
以撒
完成
fi
如果[[“$vermajor”==”| |“$org_unit”==”];然后
echo“错误:选项-v和-o需要参数。”>&2;出口1
fi
出口
这个怎么样
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
test=$(echo "$@" | grep "\-v" | grep "\-o")
if [[ -z "$test" ]]; then
printf "requirements not met.\n"
usage
fi
if [[ -n "$test" ]]; then
printf "requirements met.\n"
fi
usage(){echo“usage:$0[-v][-o]”1>&2;退出1;}
测试=$(echo“$@”grep“\-v”grep“\-o”)
如果[-z“$test”];然后
printf“未满足要求。\n”
使用
fi
如果[-n“$test”];然后
printf“满足要求。\n”
fi
输出:
bob@crunchbang:~$ ./test -v 5.0 -h
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob@crunchbang:~$ ./test -v 5.0
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob@crunchbang:~$ ./test -o "yoh"
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob@crunchbang:~$ ./test
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob@crunchbang:~$/测试-v 5.0-h
未满足要求。
用法:./试验[-v][o]
bob@crunchbang:~$/测试-v 5.0
未满足要求。
用法:./试验[-v][o]
bob@crunchbang:~$/测试-o“yoh”
未满足要求。
用法:./试验[-v][o]
bob@crunchbang:~$/测试
未满足要求。
用法:./试验[-v][o]