Rouz Rouz - 5 months ago 6
Linux Question

BASH regex check not matching on linux

I have been scripting something in BASH and I have found a strange bug. I have been developing on Mac (El Capitan) and everything is working flawlessly. But deployment on Ubuntu 16.06 server is failing and I have no idea why.

My code follows

while ! [[ ${someVariable} =~ ^[a-zA-z0-9_-]{40}$ ]]
do
read someVariable
if ! [[ ${someVariable} =~ ^[a-zA-z0-9_-]{40}$ ]];then
echo 'try again'
fi
done


for input 6LfMYB8TAAAAACRZ9bP-0GN9y4zKUYPtj255-e8A this fails. And failure happens on server only and not on the development machine. I have a feeling that I have missed something obvious.

Answer

There is a typo in your ranges. The upper case range must be from A-Z This code works on Ubuntu 14.04

while ! [[ ${someVariable} =~ ^[a-zA-Z0-9_-]{40}$ ]]
do
    read someVariable
    if ! [[ ${someVariable} =~ ^[a-zA-Z0-9_-]{40}$ ]];then  
        echo 'try again'
    fi
done
Comments