Bash For Loop Examples
I’m always forgetting the syntax to make “for” loops in Bash. I know I will have to come back here to find it, so I thought I would write put up this quick example with the hope that it will be useful to others as well.
The below example creates the lines needed for PTR records in djbdns for a small IP block. I’ll explain each part below.
for i in $(seq 70 92); do echo “^${i}.248.147.92.in-addr.arpa.:fake.domain.net.:86400″ >> file ; done
i – will be the variable
$(seq 70 92) – specifies we want the variable to be sequential to count from 70 to 92
echo “^${i}.248.147.92.in-addr.arpa.:fake.domain.net.:86400″ >> file - is the command we want to run. note the placement of ${i}, this will be where the variable counts.
results of the above script gives me a file named file. If I can it’s contents the files displays as follows:
^70.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^71.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^72.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^73.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^74.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^75.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^76.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^77.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^78.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^79.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^80.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^81.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^82.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^83.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^84.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^85.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^86.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^87.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^88.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^89.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^90.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^91.248.147.92.in-addr.arpa.:fake.domain.net.:86400
^92.248.147.92.in-addr.arpa.:fake.domain.net.:86400
Now lets say I want to verify these DNS changes were made on that server. I’d be running alot of digs again that server one by one to verify each of these changes took. Again, we can use a for loop.
for i in $(seq 70 92); do dig @mydns256.fakeserver.net +noall +answer -x 92.147.248.${i} ; done
this should display on the screen the following:
70.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
71.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
72.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
73.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
74.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
75.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
76.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
77.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
78.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
79.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
80.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
81.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
82.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
83.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
84.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
85.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
86.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
87.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
88.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
89.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
90.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
91.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
92.248.147.92.in-addr.arpa. 86400 IN PTR fake.domain.net.
This also works for file creation, another example:
$ for i in $(seq 1 10); do echo -n “file${i} “; touch file${i} 2>&1; done
$ ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9







