1

I've got a web server with a bunch of domains that have different name servers and I'm trying to clean up the mess. I'm trying to get a list of the domains and their name servers. I've got this simple script written and it almost works:

#!/bin/bash
for f in `cat mydomains.txt`
do
    echo $f " " >> mydns.txt
    dig ns $f | grep '^$f' | cut -d $'\t' -f 5 >> mydns.txt
    echo "" >> mydns.txt
done

As of right now, all this does is echo $f " " >> mydns.txt.

If I take the dig line and substitute what $f should be in the command line, I get the expected results. However, I get nothing in my script. I know that the $f variable is populated because it echoes $f in the previous line. Why doesn't it work in the script ?

perror
  • 7,071
  • 16
  • 58
  • 85
Carnivoris
  • 793
  • 3
  • 7
  • 23

3 Answers3

2

Did you mean to grep for '^$f'? It should be '^'"$f", i.e., without the ticks around the variable. It won't be expanded that way.

Sven
  • 1,748
  • 1
  • 14
  • 14
2

Single quotes prevent the shell from expanding the variable. You should use double quotes:

dig ns "$f" | grep "^$f" | ...

Note that I also used double quotes around $f in the dig call. Quoting your variables is good practice; see this other SO question for details.

Community
  • 1
  • 1
cabad
  • 4,555
  • 1
  • 20
  • 33
0

It's possible that it's a Windows-style/Unix-style newline issue. It might help to convert your newlines to \n instead of \r\n.

Have you tried the dos2unix suggestion given here: grep fails inside bash script but works on command line?

Community
  • 1
  • 1
Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
  • These files are created in and solely exist in Linux. There are no Windows newlines. – Carnivoris Oct 18 '13 at 15:19
  • Having been created on Linux doesn't necessarily mean the text editor doesn't use CRLF for newlines. Nevertheless, I'm glad you found the solution to your problem. ;) – Dan Bechard Oct 18 '13 at 18:26