I am running a script which requires the input of a MAC address from the user and was loooking for a regex that will verify the user has inputted a full 12 digit valid MAC with colons
Ive seen a few on some sites that look huge and was wondering if anybody had a one liner (or as close as possible)
echo enter mac
read mac
result=$( echo $mac | sed -n "/^\([0-9A-Z][0-9A-Z]:\)\{5\}[0-9A-Z][0-9A-Z]$/p" )
if [ -z $result ]; then
echo mac doesnt comply
else
echo mac is fine
fi
but when i run it, whatever I put in, valid or not, it comes back with the same answer
[root@my-server] # ./test-mac.sh
enter mac
00:60:08:C4:99:44
mac doesnt comply
or ...
[root@my-server] # ./test-mac.sh
enter mac
sausages
mac doesnt comply
make sure you have no leading/trailing 'spaces' once you do your 'data entry' - you're anchoring at the beginning and at the end of your 'entered' string.
ive typed it in manually still no good (no spaces at beginning or end).....my script again is ....
#!/bin/ksh
echo enter mac
read mac
result=$( echo $mac | sed -n "/^\([0-9A-Z][0-9A-Z]:\)\{5\}[0-9A-Z][0-9A-Z]$/p" )
if [ -z $result ]; then
echo mac doesnt comply
else
echo mac is fine
fi
Just out of interest if i run the command that was posted earlier at the command line
echo 00:60:08:C4:99:AA | sed -n "/^\([0-9A-Z][0-9A-Z]:\)\{5\}[0-9A-Z][0-9A-Z]$/p"
i get no output at all ...is this what is expected? ....if i remove the -n at just outputs what ever mac address i put in the begginning of the command (even if its invalid) ie....
OK this works perfectly (using egrep rather than sed)
#!/bin/ksh
echo enter mac
read mac
result=$( echo $mac | egrep ^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$ )
if [ -z $result ]; then
echo mac doesnt comply
else
echo mac is fine
fi