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