0

I have a script that requires the IP to be written correctly.

Examples:

192.168.0.1 -> echo ok
1.2.3.4 -> echo ok
4.3.a.3 -> echo Not ok
300.52.256.1 -> not ok
1.2.3.4.5 -> not ok

I've tried this:

 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
               then
                            echo SUCCES!

But this is not good enough since 300.2.3.4 is a good IP for this expression.

The only accepted IPs should be something like (0-255).(0-255).(0-255).0-255)

Bart
  • 2,151
  • 1
  • 10
  • 26

1 Answers1

1

OK,I found out the answer:

if [[ "$ip" =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]];
           then
                                echo SUCCES!

Thanks to @muru for helping!