I have a bash script which is getting options (using getopts). Let say they are ‘-n’ , ‘-d’ , ‘-u’ . I only want to have one of the option being chosen, if not, it will prompt the user error.
The code is like this:
while getopts ":dun" name; do
case $name in
d )
DELETE='YES'
;;
u )
UPDATE='YES'
;;
n )
NEW='YES'
;;
esac
done
I can only have $DELETE or $UPDATE or $NEW being ‘YES’ in one time, meaning the user cannot specific ‘-n’ and ‘-d’ in the same time or ‘-u’ and ‘-n’ in the same time, how do I achieve that in a IF statement ?
I have been looking for this in stackoverflow, but can’t find any. Thanks for the help, mate!
This is a complete hack, and depends on the option variables being either unset or “YES” (and in the form I’ve written it, is bash-only):
(If you were using the brand-x shell instead of bash, it’d be something like
if [ "$DELETE$UPDATE$NEW" = "YESYES" -o "$DELETE$UPDATE$NEW" = "YESYESYES" ]; then)