≡ Menu

Shell Script to Split IP Address into Separate Octets

The following shell script splits an IP into separate Octets into its own variables. This method can be used for any script which needs to take each octet into account, such as generating firewall rules etc…

#!/bin/bash
echo -n " Enter the IP Address (Example: 192.168.160.0): "
read IP
oct1=$(echo ${IP} | tr "." " " | awk '{ print $1 }')
oct2=$(echo ${IP} | tr "." " " | awk '{ print $2 }')
oct3=$(echo ${IP} | tr "." " " | awk '{ print $3 }')
oct4=$(echo ${IP} | tr "." " " | awk '{ print $4 }')
echo "IP Address is $oct1.$oct2.$oct3.$oct4"

Comments on this entry are closed.

  • awk -F. ‘{print $1 $2 $3 $4}’

Next post:

Previous post:

Copyright © Nish Vamadevan 2002-2017. All Rights Reserved. Terms and Policies.