bash string compare to multiple correct values

eagle00789 picture eagle00789 · Jan 16, 2014 · Viewed 113.1k times · Source

i have the following piece of bashscript:

function get_cms {
    echo "input cms name"
    read cms
    cms=${cms,,}
    if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then
        get_cms
    fi
}

But no matter what i input (correct and incorrect values) it never calls the function again, because I only want to allow 1 of those 3 inputs. I have tried it with || with [ var != value ] or [ var != value1 ] or [ var != value1 ] but nothing works. Can someone point me in the right direction?

Answer

Edgar Grill picture Edgar Grill · May 24, 2016

If the main intent is to check whether the supplied value is not found in a list, maybe you can use the extended regular expression matching built in BASH via the "equal tilde" operator (see also this answer):

if ! [[ "$cms" =~ ^(wordpress|meganto|typo3)$ ]]; then get_cms ; fi

Have a nice day