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
  • 95,415
  • 23
  • 151
  • 231
Piduna
  • 145
  • 2
  • 12

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
  • 216,930
  • 22
  • 195
  • 267
  • @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
  • 190,037
  • 45
  • 260
  • 285