Error in passing parameter to a Shell Script

Asked

Viewed 397 times

0

I am creating a script to take some images in the folder and with that generate another image, I am using Imagemagick, but the problem I am having is in passing one of the parameters of this script that I am mounting 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

1 answer

2


The problem is in the last line of the script:

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

Everything inside simple quotes 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
  • Valew, it worked, now I’ll study these changes, but I’m still studying Shell script

Browser other questions tagged

You are not signed in. Login or sign up in order to post.