Question

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.

Was it helpful?

Solution

Use eval

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

OTHER TIPS

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"

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top