mercredi 29 avril 2020

How to test a dynamic substring variable in a conditional in shellscript?

This code check if the last 4 characters of the variable $1 correspond to the string in variable $2 anticipated by a dot.

    if [ "${1: -4}" == ".$2" ]; then
        return 0
    else
        return 1
    fi

// true with $1 = example.doc and $2 = doc
// false with $1 = example.docx and $2 = doc

how can I replace the hardcoded 4 with the following variable $CHECKER_EXTENSION calculated like this?

    LENGTH_EXTENSION=${#2}
    CHECKER_EXTENSION=$(( LENGTH_EXTENSION + 1 ))

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire