When writing a bash script to help create a polaroid sketch using the Imagick convert command. I ran into a problem. Although I manage to get around this (actually, since convert is flexible enough), I still want to know how to solve this problem without such a workaround.
Basically, a bash script will get a header value that may contain a space. I want to use this header as a convert parameter. If the title is empty (''), I will not use the '-caption' parameter for the 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 repeats the value command, which can be copied, inserted into the terminal, and started. BUT bash does not start. How can i do this?
NOTE. In the case of convert , -caption "" do the job. I know this and am currently using it as a job.
Thanks in advance for your help.
EDIT: From the answer, here is the code that works 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 this will be useful for those looking for a similar solution.
bash
Nawaman
source share