≡ 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"
1 comment… add one

Leave a Comment

Next post:

Previous post:

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