[RndTbl] Fix resolv.conf on WSL instantiation...

Trevor Cordes trevor at tecnopolis.ca
Wed Jun 3 01:10:37 CDT 2020

On 2020-06-02 Sean Cody wrote:
> To:
> nameserver <mine>
> nameserver <$work1>
> nameserver <$work2>
> search <$workdomain>
> Ideally the solution would note if there 3 or more nameserver lines,
> move the lines before ‘nameserver <$work1>’ to one line before the
> last file. I’d pop that into my .bashrc/.profile or similar.

While you're waiting for MS to fix it... perl can do it in an under-200
chars one-liner.  I'm assuming WSL lets you have perl!

perl -i -e 'while(<>){ $s.=$_; if (eof ARGV){ $work1="ns.foo.com"; $_=$s; exit if !/(?:nameserver [^\n]+\n){3}/s; s/(.*?\n)?(nameserver \Q$work1\E\n)(.*?\n)([^\n]+\n?)$/$2$3$1$4/s; print } }' /etc/resolv.conf

Just change the $work1 var in there.  (tcsh users will have to escape
the ! also.)  Uses perl's inline editing (-i) but you have to do
trickery to operate on full file (necessary here), hence all the
weirdness and having to write my own loop rather than use -n.

As you requested:
- will not run unless 3+ nameserver lines
- $work1 line should be able to be on any line before the search,
  but I only tested your file test case
- the magic happens in the $2$3$1$4 if you need to alter the
  line ordering: but note, the numbers don't correspond to lines,
  they correspond to semantic groups: 1. pre-work1, 2. work1, 3.
  post-work1 but not final line, 4. final line

P.S. Your original wording was a bit wonky ("one line before the
last file") which I took to mean one line before the last *line*.

More information about the Roundtable mailing list