Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

`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

Replies are listed 'Best First'.
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!


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

    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940452]
Approved by keszler
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-06-25 12:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.