nixcraft has a link on how to read a file line by line. The method is a great way to read a file, but there some trouble spots I thought I would point out.
In the script, the special variable IFS is set:
# set the Internal Field Separator to a pipe symbol
IFS='|'
The tells the read command to split “cyberciti.biz|74.86.48.99″ into “cyberciti.biz” and “74.86.48.99″ and thus fill both the domain and ip variables here:
while read domain ip
Using BASH to split strings is much faster than doing something line this:
while read line
do
domain=$(echo $line | awk -F'|' '{print $1}'
ip=$(echo $line | awk -F'|' '{print $2}'
As new script writers typically do. However, setting IFS and forgetting to reset the special variable can cause some odd problems in longer scripts. For example, lets say you needed to read a second file, later on in the script. This one delimited by spaces. For simplicity, I will take the same file and just replace the pipe characters with spaces.
/tmp/domains-using-space.txt
root@b92 [~]# cat /tmp/domains-using-space.txt
cyberciti.biz 74.86.48.99
nixcraft.com 75.126.168.152
theos.in 75.126.168.153
cricketnow.in 75.126.168.154
vivekgite.com 75.126.168.155
Now, here is my new script:
#!/bin/ksh
# set the Internal Field Separator to a pipe symbol
IFS='|'
# file name
file=/tmp/domains.txt
# use while loop to read domain and ip
while read domain ip
do
print "$domain has address $ip"
done










don't forget to vote if you find something useful!!
42 weeks 4 days ago
43 weeks 1 day ago
46 weeks 12 hours ago
48 weeks 4 days ago
48 weeks 4 days ago
48 weeks 5 days ago
48 weeks 5 days ago
1 year 2 days ago
1 year 2 weeks ago
1 year 2 weeks ago