Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

`hostname -s`

by joeymac (Acolyte)
on Nov 28, 2011 at 19:41 UTC ( #940452=perlquestion: print w/ replies, xml ) Need Help??
joeymac has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I had a colleague ask me a question about some Perl code he was writing. The bit he was having trouble with involves using the hostname UNIX command. If the $HOSTNAME is hard coded (commented out line below) everything works fine. However if he tries to use

 $HOSTNAME = `hostname -s`;

it doesn't work right. From my playing around with the bit of code below, there seems to be some problem matching the string with what is returned by the hostname command. The code never gets to any of the $ANCHOR's but $HOSTNAME seems to be passed as a string properly (see output below). Any help would be greatly appreciated! Thanks!! Here's the code snippet:

#!/bin/perl $HOSTNAME = `hostname -s`; #$HOSTNAME = server2; if ($HOSTNAME eq server1) { $ANCHOR = "/HOME/"; } elsif ($HOSTNAME eq server2) { $ANCHOR = "/TEST/HOME"; } elsif ($HOSTNAME eq server3) { $ANCHOR = "/DEV/HOME"; } print "Anchor is $ANCHOR on host $HOSTNAME\n";

OUTPUT w/ `hostname -s`:

Anchor is  on host server2

OUTPUT w/ hard code:

Anchor is /TEST/HOME on host server2

Comment on `hostname -s`
Select or Download Code
Re: `hostname -s`
by keszler (Priest) on Nov 28, 2011 at 19:47 UTC
    $HOSTNAME = `hostname -s`;

    $HOSTNAME includes "\n". Chomp it.

    Update: Here's a way to see what's happening:

    prompt$ perl -e '$HOSTNAME = `hostname -s`; print $HOSTNAME;' | od -c

      Ah! Yes of course! I tried out the update you included. That is interesting that it includes the "\n". I fixed the code

      chomp($HOSTNAME = `hostname -s`);

      and it works great now! Tried it on all three servers and it was successful. I'm going now to relay the fix to my coworker. Thanks for your help!

      Joe

Re: `hostname -s`
by toolic (Chancellor) on Nov 28, 2011 at 19:59 UTC
Re: `hostname -s`
by Corion (Pope) on Nov 28, 2011 at 20:06 UTC
Re: `hostname -s`
by i5513 (Monk) on Nov 28, 2011 at 22:22 UTC
    WARNING:

    Don't use it in Solaris! Or your new hostname will be named "-s" :)

    So use module suggested by Corion

Re: `hostname -s`
by graff (Chancellor) on Nov 29, 2011 at 00:26 UTC
    Apart from using the Sys::Hostname module as recommended by Corion, you could also check to see whether the shell environment in which perl is operating contains a "HOST" variable already set for you:
    perl -le 'print $ENV{HOST}'
    That works for me when running on a linux (debian) desktop machine (but not on a macosx 10.7 laptop). For the sake of portability, I'd go with the module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940452]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-08-29 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls