bash regex


Results 1 to 1 of 1

Thread: bash regex

  1. #1
    Join Date
    Jul 2002
    Location
    Chesapeake, VA
    Posts
    250

    bash regex

    I am trying to make sure that the number entered for the barcode variable is exactly 10 digits long but it doesnt work and get the following error. What am I doing wrong?

    error: ./regex.sh: line 6: [: =~: binary operator expected

    Code:
    if [ ${barcode} =~ "[0-9]{10}" ]
    then
            echo "Regex Passes!"
    else
            echo "Regex Failed!"
    fi
    If I change the =~ to == I don't get an error but it still fails when it shouldnt, I assume thats because I am then comparing 2 different numbers for equality, rather than testing for an acceptable pattern.

    I did get the following to work but it wasn't what I was going for.. also it allows numbers longer than 10 digits to be accepted.

    Code:
    if echo ${barcode} | grep -Eq '[0-9]{10}'
    then
            echo "Regex Passes!"
    else
            echo "Regex Failed!"
    fi
    Last edited by jiggolo182; 05-22-2013 at 09:33 AM. Reason: update my results

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •