Skip to main content
semi-gratuitous spell-check of the variable name
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

The lazy way is just to run

if [[ $mac_adress$mac_address == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress$mac_address =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortened to

if [[ $mac_adress$mac_address =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortened to

if [[ $mac_adress =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_address == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_address =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortened to

if [[ $mac_address =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

I don't think those parenthesis are required
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortendshortened to

if [[ $mac_adress =~ ^(([[:xdigit:]]){2}:){5}([[:xdigit:]]){2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortend to

if [[ $mac_adress =~ ^(([[:xdigit:]]){2}:){5}([[:xdigit:]]){2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortened to

if [[ $mac_adress =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

added 2 characters in body
Source Link
nohillside
  • 3.3k
  • 20
  • 27

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ [0^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]F]$ ]]; then echo Heureka; fi

might be better. The later can be shortend to

if [[ $mac_adress =~ ^(([0-9a-fA-F][[:xdigit:]]){2}:){5}([0-9a-fA-F][[:xdigit:]]){2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F] ]]; then echo Heureka; fi

might be better. The later can be shortend to

if [[ $mac_adress =~ (([0-9a-fA-F]){2}:){5}([0-9a-fA-F]){2} ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

The lazy way is just to run

if [[ $mac_adress == ??:??:??:??:??:?? ]]; then echo Heureka; fi

but this doesn't check whether it's a hex string. So if this is important

if [[ $mac_adress =~ ^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$ ]]; then echo Heureka; fi

might be better. The later can be shortend to

if [[ $mac_adress =~ ^(([[:xdigit:]]){2}:){5}([[:xdigit:]]){2}$ ]]; then
    echo Heureka; 
fi

If the pattern matches I don't see a need to check for the correct length as well.

Source Link
nohillside
  • 3.3k
  • 20
  • 27
Loading