title: In bash, how do I determine if only one of a set of variable has a value?
body: <p>I have three variables, let's call them <code>DATE</code>, <code>URI</code> and <code>URI_FILE</code>. They contain the results of processing the command line with <code>getopt</code>:</p>
  
  <pre><code>while getopts ":d:p:u:hFA" opt; do
  
     case $opt in
         d) DATE=$OPTARG;;
         u) URI=${OPTARG};;
         U) URI_FILE=${OPTARG};;
         F) FORCE=yes;;
         A) ALL=yes;;
  
     esac
  
  done
  </code></pre>
  
  <p>I need to fail with an error if more than one of those variables are set to any value (they are alternative command line options). I'm completely clueless about how to test that elegantly. I have something that looks like:</p>
  
  <pre><code>#!/bin/bash 
  
  if [[ -z "${URI+x}" ]]
  then
      URI_PRESENT=0
  else
      URI_PRESENT=1
  fi
  
  if [[ -z "${URI_FILE+x}" ]]
  then
      URI_FILE_PRESENT=0
  else
      URI_FILE_PRESENT=1
  fi
  
  if [[ -z "${DATE+x}" ]]
  then
      DATE_PRESENT=0
  else
      DATE_PRESENT=1
  fi
  
  
  ARGCOUNT=$((${DATE_PRESENT} + ${URI_FILE_PRESENT} + ${URI_PRESENT}))
  
  if (( ${ARGCOUNT} &gt; 1 ))
  then
      echo "Only one of date, uri and uri-file may be supplied"
      exit 1
  fi
  </code></pre>
  
  <p>There has to be a cleaner way to do this. Suggestions? I'm going to be adding at least on more alternative and this will get painful</p>