1

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.

Zippyduda
  • 107
  • 2
  • 7
  • 19

3 Answers3

1

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"
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • I thought so. It was just because I have it to look for A, AAAA, MX and TXT so I wanted to have one variable to edit (more clean) instead of all four greps to have | grep -v 'DiG\|;' after them. – Zippyduda Feb 26 '13 at 11:45
1

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"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This doesn't work either, just returns: syntax error: unexpected end of file – Zippyduda Feb 26 '13 at 16:16
  • Are you trying to put the function definition on one line? If so, a semi-colon is required between the command and the closing brace. – chepner Feb 26 '13 at 16:40
  • Ah, I had a feeling that would be necessary. Will give it a go even though I have found a solution :) tah. – Zippyduda Feb 26 '13 at 17:32
1

Use eval

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