Hi I'm new to Perl. The reason for the program is part of a procedure to ensure that the configured server IP addrss(obtained from ifconfig) is the same as that contained within the hosts file. The code has been
checked by some more experienced Perl programmers and is shown below. All I want to extract is the first IP address
for fred in the hosts file also shown below
#!/usr/bin/perl
my $HN = `hostname`;
my $hosts =`cat /etc/hosts`;
if ( $hosts =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+(${$HN})\s/g )
{
print "$1\n";
print "$2\n";
}
This program prints the IP address but not the host name ($2) as defined( I thought) by the second set of
parentheses (${$HN})-- Without the ${$HN} NO match is made at all. I know I already have the hostname ($HN)
but dont understand why $2 is not set to $HN
The format of the hosts file(or at least the entries I am interested in) is as follows
x.y.z.22 fred<space or tab>localhost
x.y.z.23 fred-test-0
x.y.z.24 fred-test-1
Can you explain a) why $2 is not populated and b) why just supplying ($HN) instead of (${$HN}) does not work
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.