Friday, 23 August 2013

IF statement in BASH isn't doing what's expected

IF statement in BASH isn't doing what's expected

Got a very simple script which checks barcodes basically. There's two
barcodes that have to be checked that they are not confused when being
made into a variable.
Basically the first barcode should contain only numbers 0-9, and the
second barcode should contain two letters, then some numbers, then two
more letters, like AB123456789CD.
If they're confused and read in the wrong order then it plays an error
sound. This is what I have so far, the top one's working, but I'm not sure
it it's the best solution, and the bottom one doesn't do what I want:
echo -e $BLUE"Please scan the first barcode"$ENDCOLOUR
read -p "Barcode: " BARCODE1
if [[ "$BARCODE1" =~ [a-z] ]] ; then
play -q ./error.wav
else
echo -e $BLUE"Please scan the second barcode"$ENDCOLOUR
read -p "Barcode: " BARCODE1
if [[ "$BARCODE2" =~ [a-z0-9] ]] ; then
play -q ./error.wav
else
echo "'$BARCODE1',$BARCODE2'" >> barcodes.csv
fi
fi
What's wrong? And is there a more optimal means of achieving this?

No comments:

Post a Comment