Вопрос

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.

Это было полезно?

Решение

Use eval

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

Другие советы

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"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top