tags:

views:

170

answers:

4

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.

+7  A: 

You'll want to read entry 050 in the BASH FAQ.

Ignacio Vazquez-Abrams
Thanks for answering. I try it and it works :D BUT only in terminal. When I put it in bash file the command `ARGS=("-caption" "$CAPTION")` failed with an error message of './polaroid.sh: 19: Syntax error: "(" unexpected'. Do you know what this is?
NawaMan
Use `bash` in your shebang line, not `sh`.
Ignacio Vazquez-Abrams
What a shame? :( Thanks for pointing that out anyway. I get it to work now. :D
NawaMan
A: 
CAPTION="$1"
IN_FILE="resources/puppy.png"
OUTFILE="resources/puppy_polaroid.png"

case "$CAPTION" in
  "" ) CAPTION="-caption ''";;
  * ) CAPTION='-caption "$CAPTION"';;
esac

convert $CAPTION "$IN_FILE" "$OUTFILE"
ghostdog74
This is no different from what I am doing and it does not work. Thanks anyway.
NawaMan
are you sure it does not work? what does not work?
ghostdog74
I am sure. It still split my caption by spaces.
NawaMan
ghostdog - how _would_ it work when $1 contains whitespace?
Charles Duffy
first, the parameter passed to the script should be quoted. And then use quotes to preserve spaces.
ghostdog74
+1  A: 

Putting backticks around $COMMAND on the last line causes the script to try to execute the output of the command rather than the command itself.

$ c='echo hi'
$ `$c`
hi: command not found

This will work:

if [[ "$CAPTION" != "" ]]
then
    convert -caption "$CAPTION" "$IN_FILE" "$OUTFILE"
else
    convert "$IN_FILE" "$OUTFILE"
fi
Dennis Williamson
While this solution is valid, it is not practical. Imagine if I have many more parameter I have to process in the similar way as caption for example, background color, border color, rotation degree, and so on. We just cannot create IF for all the missing parameter. Thanks anyway. :-D
NawaMan
+1  A: 

Use an array, as so:

#!/bin/bash
# ^^^ - note the shebang line explicitly using bash, not /bin/sh

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"

extra_args=( )
if [[ $CAPTION ]] ; then
  extra_args+=( -caption "$1" )
fi
convert "${extra_args[@]}" "$INFILE" "$OUTFILE"

This construct presumes that you're potentially going to be appending numerous extra arguments. Note that += is unsupported in some older versions of bash which are still present on systems deployed in the field (most notably RHEL4). For such older releases it can be necessary to write extra_args=( "${extra_args[@]}" -caption "$1" ) to append to an array.

Charles Duffy