Zippyduda
Zippyduda

Reputation: 107

dig nameserver domain | $variable doesn't work

What am I doing wrong?

This works:

ns="ns.nameserver.co.uk"
d="domain.co.uk"
dig @$ns $d A | grep $d

However using just a variable after pipe does not (it hangs):

ns="ns.nameserver.co.uk"
d="domain.co.uk"
g=$(grep $d | grep -v "DiG")
dig @$ns $d A | $g

Do I need to do something special after the pipe so it knows to run the grep command from the g variable? Using backticks (historic) fails as well.

Upvotes: 1

Views: 789

Answers (3)

Eduardo G
Eduardo G

Reputation: 77

Use eval

ns="ns.nameserver.co.uk"
d="domain.co.uk"
g="grep $d | grep -v 'DiG'"
dig @$ns $d A | eval $g

Upvotes: 1

chepner
chepner

Reputation: 530853

You can define a function instead of a variable.

ns="ns.nameserver.co.uk"
d="domain.co.uk"
g () {
    grep "$1" | grep -v "DiG"
}
dig @$ns $d A | g "$d"

Upvotes: 1

Michael Mior
Michael Mior

Reputation: 28753

You can't store a command in a variable, only the output of a command. Since you haven't specified any input to grep on the third line, it will read from standard input. You can simply remove the variable and change the dig command to the following

dig @$ns $d A | grep $d | grep -v "DiG"

Upvotes: 1

Related Questions