How to safely specify an optional flag?

If installed $FOO, I want to run:

cd "$OUTPUTDIR" && fpm -s dir -t rpm \
  -a x86_64 \
  --epoch "${PKGEPOCH}" \
  -n "${PACKAGENAME}" \
  --version "${PKGVERSION}" \
  --iteration "${PKGRELEASE}" \
  -C "$OUTPUTDIR/installroot" \
  --description="${PKGDESCRIPTION}" \
  .

If $FOOnot set, I do not want to enable the flag at all.

The program crashes if --description=(empty).

However, sometimes descriptions include quotation marks and other special characters, so I don't want to do:

if [[ -z "PKGDESCRIPTION" ]]; then
    D=--description="${PKGDESCRIPTION}"
fi
cd "$OUTPUTDIR" && fpm -s dir -t rpm \
  -a x86_64 \
  --epoch "${PKGEPOCH}" \
  -n "${PACKAGENAME}" \
  --version "${PKGVERSION}" \
  --iteration "${PKGRELEASE}" \
  -C "$OUTPUTDIR/installroot" \
  $D
  .

If I put quotes around $D, then it becomes an extra (empty) argument.

Is there a way to do this that would not be a security issue if it $PKGDESCRIPTIONcontains special characters and does not generate an empty argument?

+4
source share
2 answers

PKGDESCRIPTION , , " ":

[...] && fpm -s dir -t rpm \
    [...] \
    ${PKGDESCRIPTION:+ --description="${PKGDESCRIPTION}"} \
    .

: :+ , , PKGDESCRIPTION , ; - , --description="${PKGDESCRIPTION}", PKGDESCRIPTION. , :+ -- , , , .

, , @glenn jackman.

+2

- :

options=( -a x86_64  -C "$OUTPUTDIR/installroot" )
[[ $PKGEPOCH ]]       && options+=( --epoch "$PGKEPOCH" )
[[ $PACKAGENAME ]]    && options+=( -n "$PACKAGENAME" )
[[ $PKGVERSION ]]     && options+=( --version "$PKGVERSION" )
[[ $PKGRELEASE ]]     && options+=( --iteration "$PKGRELEASE" )
[[ $PKGDESCRIPTION ]] && options+=( --description="$PKGDESCRIPTION" )

cd "$OUTPUTDIR" && fpm -s dir -t rpm "${options[@]}"

. http://mywiki.wooledge.org/BashFAQ/050

+9

All Articles