I'm unable to get a pattern to match properly using regex in bash 4.1. I've read some information on about differences in quotes vs not quotes but I don't think that is my problem.
My goal is to check and make sure that the script is provided a valid ID. A valid ID in this case is a string of 9 digits. It's my understand that a regex expression for that is \d{9}. Given that here is my code snippet to check:
id=$1
if [[ $id =~ \d{9} ]]; then
echo "This is a vaild ID"
else
echo "This is not a vaild ID"
fi
Then call the script with:
./script 987654321
Something obvious that I am missing?
\d
, you can use[0-9]
or the[:digit:]
posix character class. – Philliecase $id in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo fine ;; *) die in anguish;; esac
– Tremolant