≡ Menu

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"

For i in For loop from File

The file “test” has the following…

:~$ cat test
1
2
3
4
5
:~$
for i in `cat test`; do host ns$i.he.net; done

Output

:~$ for i in `cat test`; do host ns$i.he.net; done
ns1.he.net has address 216.218.130.2
ns2.he.net has address 216.218.131.2
ns2.he.net has IPv6 address 2001:470:200::2
ns3.he.net has address 216.218.132.2
ns3.he.net has IPv6 address 2001:470:300::2
ns4.he.net has address 216.66.1.2
ns4.he.net has IPv6 address 2001:470:400::2
ns5.he.net has address 216.66.80.18
ns5.he.net has IPv6 address 2001:470:500::2
:~$

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