我有一个简单的脚本(如下),它具有互斥的参数。getopts检查互斥参数
该脚本的参数应为./scriptname.sh -m|-d [-n]
,但是,用户可以使用./scriptname.sh -m -d
运行脚本,这是错误的。
问题:我该如何强制只有一个互斥参数由用户提供?
#!/bin/bash
progname=$0
function usage() {
cat <<EOF
Usage: $progname -m|-d [-n]
where:
-m create minimal box
-d create desktop box
-n perform headless build
EOF
exit 0
}
headless='false'
buildtype=''
while getopts 'mdnh' flag; do
case "${flag}" in
m) buildtype='minimal' ;;
d) buildtype='desktop' ;;
n) headless='true' ;;
h) usage ;;
\?) usage ;;
*) usage ;;
esac
done
if [ "$buildtype" == "" ];
then
usage
exit
fi
别人已经问这个......等一下,我会找到它......啊,它就在那里.. 。[如何在你的shell中使用互斥的标志 - 坚持'getopts'?](http://stackoverflow.com/questions/21695565/how-to-use-mutually-exclusive-flags-in-your-shell-卡住与getopts)甚至在相关问题的列表。你也可以看看[在bash shell脚本中使用'getopts'来获取长命令行和短命令行选项](http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-获得长和短的命令行选项/)。这涵盖了很多领域。 –
我在谷歌上搜索并扫描推荐的问题,但并不认为这些链接是相关的。希望我的问题标题能够帮助那些在搜索方面不够好的人(如我): –