我有一個簡單的腳本(如下),它具有互斥的參數。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-獲得長和短的命令行選項/)。這涵蓋了很多領域。 –
我在谷歌上搜索並掃描推薦的問題,但並不認爲這些鏈接是相關的。希望我的問題標題能夠幫助那些在搜索方面不夠好的人(如我): –