While writing a bash script to help creating polaroid thumbnail using Imagick's convert
commmand. I encounter a problem. Although, I manage to work around with this (actually, because convert
is flexible enough), I still want to know how to solve this without such specific workaround.
So basically, the bash script will get a caption value which may contain space. I want to use that caption as parameter of convert
. If the caption is empty (''), I will not use the option '-caption' for convert command. Like this:
CAPTION="Is this Cute?" # The actual value will be tacked from the parameter of this bash.
IN_FILE="resources/puppy.png"
OUTFILE="resources/puppy_polaroid.png"
# If CAPTION is not empty, reformat CAPTION
if [ "$CAPTION" != "" ]; then CAPTION="-caption \"$CAPTION\""; fi
# otherwise, do not use '-caption' add all
COMMAND="convert $CAPTION \"$IN_FILE\" \"$OUTFILE\""
echo "Command: $COMMAND" #This echo a value command
`$COMMAND`
The echo echoes the value command that can be copied can pasted in a terminal and run. BUT the bash does not run. How I can do this?
NOTE: In case of convert
, -caption ""
do the job. I know this and currently use this as work around.
Thanks in advance for helps.
EDIT: From the answer, here is the code that work for me now.
... # Get CAPTION and GRAVITY from parameters
if [ "$CAPTION" != "" ]; then ARGS_CAPTION=(-caption "$CAPTION"); fi
if [ "$GRAVITY" != "" ]; then ARGS_GRAVITY=(-gravity "$GRAVITY"); fi
if [ ! -f "$IN_FILE" ]; then echo "The input file does not exist: '$IN_FILE'"; exit; fi
if [ "$OUTFILE" == "" ]; then OUTFILE=${IN_FILE%.*}-${IN_FILE#*.}-polaroid.png; fi
ARGS=("${ARGS_CAPTION[@]}" -thumbnail 480x480 -border 5x5 -pointsize 60 "${ARGS_GRAVITY[@]}" +polaroid -thumbnail 120x120)
echo convert "${ARGS[@]}" "$IN_FILE" "$OUTFILE";
convert "${ARGS[@]}" "$IN_FILE" "$OUTFILE"
I hope that this will be useful for those seeking similar solution.