1

I have script for getting some records:

#!/bin/bash 

host_start=test
domain=test.com

for host in "${host_start}"{1..200}."$domain";
do
    address=`dig +short $host`
    echo "$address = $host"
done

In this case, everything is OK. I have:

192.168.1.1 = test1.test.com
192.168.1.2 = test2.test.com
192.168.1.3 = test3.test.com
...
...
...
etc ...

But instead of the literal {1..200}, I want to use variables in the start of my script. I did this:

t1=1
t2=200
for host in "${host_start}"{$t1..$t2}."$domain";
do 
...

In this case, I get an error:

dig: 'test{1..200}.test.com' is not a legal name (empty label)

Where is my error? How do I fix it?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Piduna
  • 165
  • 3
  • 14

2 Answers2

3

Brace expansion happens before variable expansion, so you can't use it with variables. Use a loop or the seq command.

for ((i=t1; i<=t2; i++)) ; do
    host=$host_start$i.$domain

or

for i in $( seq $t1 $t2 ) ; do
    host=$host_start$i.$domain
choroba
  • 231,213
  • 25
  • 204
  • 289
  • @MadPhysicist: `seq` is a nonstandard utility, so if strict POSIX compliance is a must, it should be avoided; it is, however, both widely available, widely used, and continues to provide useful functionality. – mklement0 May 16 '17 at 13:34
  • Fair enough. Comment rescinded. – Mad Physicist May 16 '17 at 13:36
2

You should probably do:

for ((i=t1; i <= t2; i++)); do 
   host="${host_start}"$i."$domain"
   ...
done
William Pursell
  • 204,365
  • 48
  • 270
  • 300