If the requirement is that only digits may occur, you can test for the negation of a string of 1 or more digits instead:
if ! [[ $lastseen =~ ^[0-9]+$ ]]; then
echo "Serial Number is invalid: it does not consist entirely of digits"
With regular patterns you could use:
case $lastseen in
echo "Serial Number Invalid: it does not consist entirely of digits"
Originally Posted by MadeInGermany
With a shell glob you need two conditions
if [[ -z $lastseen ]] || [[ $lastseen == *[_a-zA-Z]* ]]
echo "empty or has an alpha character"
if [[ -z $lastseen ]] || [[ $lastseen == *[!0-9]* ]]
echo "empty or has a non-digit character"
You can use one condition like so:
if [[ -z $lastseen || $lastseen == *[_a-zA-Z]* ]]; then
Originally Posted by Yoda
In bash, for regular expression matching, you have to use a =~ [..]
So you can try:-
if [[ "$lastseen" =~ '^[a-ZA-Z]*$' ]] && [ ! -z "$lastseen" ]
echo "Serial Number Valid"
echo "Serial Number Invaild"
To use regexes you need to leave them unquoted. But even without the quotes, this would seem to do the opposite of what is required. The left condition tests whether a string consists exclusively of alpha characters (letters) or is empty, whereas the requirement is that no non-digits may occur.
Last edited by Scrutinizer; 12-11-2017 at 10:45 PM..
The Following User Says Thank You to Scrutinizer For This Useful Post: