Error in parameter passing to a Shell Script

0

I'm creating a script to get some images in the folder and with this generate another image, I'm using Imagemagick, but the problem I'm having is in passing one of the parameters of this script I'm putting together and gives me this error:

montage: unrecognized color '$BACKGROUND' @ warning/color.c/GetColorCompliance/1046.

Basically, I call the script and pass the color parameter, like this: ./meuScript.sh -b 00FFFF

and the script is this, I left only the necessary

#!/bin/bash

MSG_USO="
Uso: $(basename "$0") [-h | -d | -b ]
-b | --background  cor de fundo da imagem gerada
"

BACKGROUND="4E9A06"

while test -n "$1"
do
    case $1 in
        -b | --background)
            shift
            BACKGROUND="$1"
        ;;

    esac

    # opção $1 já processada, a fila deve andar
    shift
done

# remove montagem existente
rm Montagem.png
# cria nova montagem
montage -background '#"$BACKGROUND"' -geometry +4+4 *.jpg Montagem.png
    
asked by anonymous 25.01.2017 / 04:08

1 answer

2

The problem is in the last line of the script:

$ montage -background '#"$BACKGROUND"' -geometry +4+4 *.jpg Montagem.png

Everything within single quotation marks is preserved, without exception. See the difference:

$ echo '#"$BACKGROUND"'
$ echo "#$BACKGROUND"

Output:

$ #"$BACKGROUND"
$ #00FFFF

So just change to:

$ montage -background "#$BACKGROUND" -geometry +4+4 *.jpg Montagem.png

I also suggest some modifications:

#!/bin/bash

background="4E9A06"
output_name="Montagem.png"

function remove_old_file() {
    if [ -f "$output_name" ]; then
        rm $output_name
    fi
}

function generate_image() {
    montage -background "#$background" -geometry +4+4 *.jpg "$output_name"
}

function usage() {
    echo "Uso: $(basename $0) [-h | -d | -b ]"
    echo "-b | --background  cor de fundo da imagem gerada"
}

function run() {
    remove_old_file

    while test -n "$1"; do
        case $1 in
            -b | --background)
                shift
                background="$1"
            ;;
        esac

        shift # opção $1 já processada, a fila deve andar
    done

    generate_image
}

if [ -z "$1" ]; then
    usage
else
    run $*
fi
    
25.01.2017 / 04:43