1

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.

3
  • What @James says. But also, possible different versions of bash (bash --version)? Commented Jun 23, 2016 at 20:09
  • 1
    A deleted answer mentions the suspicious-looking range A-z. Please confirm that your script continues to fail if you replace that with A-Z. Commented Jun 23, 2016 at 20:45
  • The range was bad, it was a simple typo. Lucky for me was that i copied the whole if. If I had typed it again here I would have not make the same mistake Commented Jun 23, 2016 at 20:58

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

This. The reason why OP is seeing differing results for A-z is due to the locale. It would accidentally work for LC_COLLATE=C (the default) and POSIX, but would fail for systems using e.g. en_US.utf8.
yes! the problem was caused by simple typo... I feel silly sometimes. Thank you!
1

try this regex?

^[-_a-zA-Z0-9]{40}$

2 Comments

Although my problem was due to simple typo I have failed to see (range was A-z instead A-Z) I would like to know why would this regex be better and, of course, why would you suggest this solution. Is this more efficient or...?
oh i just typed out what I thought would work into a terminal. I figured you would do the diff and figure out what went wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.