How to conditionally add flags to shell scripts?

#!/bin/bash

/usr/local/bin/mongo-connector \
  -m "$MONGO_HOST" \
  -t "$NEO_URI" \
  ${VERBOSE:+-v} \
  -stdout

If VERBOSE is set and non-empty, then ${VERBOSE:+-v} evaluates to -v. If VERBOSE is unset or empty, it evaluates to the empty string. Note that this is an instance where you must avoid using double quotes. If you write: cmd "${VERBOSE:+-v}" rather than cmd ${VERBOSE+:v}, the behavior is semantically different when VERBOSE is empty or unset. In the former case, cmd is called with one argument (the empty string), while in the latter case cmd is called with zero arguments.

To test if VERBOSE is set to a particular string, you can do things like:

/usr/local/bin/mongo-connector \
  -m "$MONGO_HOST" \
  -t "$NEO_URI" \
  $(case ${VERBOSE} in (v1) printf -- '-v foo';; (v2) printf -- '-v bar';; esac )\
  -stdout

but it seems cleaner to write that as

case "${VERBOSE}" in
v1) varg=foo;;
v2) varg=bar;;
*)  unset varg;;
esac
/usr/local/bin/mongo-connector -m "$MONGO_HOST" -t "$NEO_URI" \
      ${varg+-v "$varg"} -stdout

Again, note the use of double quotes here. You want them around $varg to ensure that only one string is passed, and you do not want "${varg+-v "$varg"}" because you want to ensure that you do not pass an empty string. Also, since the case statement explicitly unsets varg, we can now omit the :.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)