Command substitution in perl

Discussion in 'Perl Misc' started by kp, Oct 7, 2006.

  1. kp

    kp Guest

    My perl module file includes this line:
    $HOSTNAME = "/usr/bin/hostname";

    I have sourced this perl module file in my perl script.

    In my perl script I have an if loop :
    if ( `$HOSTNAME` eq "rsd2" ) {
    print "NFS server is fsd2";
    }


    However, this command substitution does not seem to work.

    Any clues???

    -kp
    kp, Oct 7, 2006
    #1
    1. Advertising

  2. kp

    Ala Qumsieh Guest

    kp wrote:

    > My perl module file includes this line:
    > $HOSTNAME = "/usr/bin/hostname";
    >
    > I have sourced this perl module file in my perl script.
    >
    > In my perl script I have an if loop :
    > if ( `$HOSTNAME` eq "rsd2" ) {
    > print "NFS server is fsd2";
    > }
    >
    >
    > However, this command substitution does not seem to work.
    >
    > Any clues???


    perldoc -q host

    --Ala
    Ala Qumsieh, Oct 7, 2006
    #2
    1. Advertising

  3. kp wrote:
    > My perl module file includes this line:
    > $HOSTNAME = "/usr/bin/hostname";
    >
    > I have sourced this perl module file in my perl script.
    >
    > In my perl script I have an if loop :
    > if ( `$HOSTNAME` eq "rsd2" ) {
    > print "NFS server is fsd2";
    > }
    >
    > However, this command substitution does not seem to work.


    Try printing the result of the call, most likely it is not what you think it
    is.
    print `$HOSTNAME`;
    Hint: I bet there is at least a newline at the end

    jue
    Jürgen Exner, Oct 7, 2006
    #3
  4. kp

    Tintin Guest

    "kp" <> wrote in message
    news:...
    > My perl module file includes this line:
    > $HOSTNAME = "/usr/bin/hostname";
    >
    > I have sourced this perl module file in my perl script.
    >
    > In my perl script I have an if loop :
    > if ( `$HOSTNAME` eq "rsd2" ) {
    > print "NFS server is fsd2";
    > }
    >
    >
    > However, this command substitution does not seem to work.
    >
    > Any clues???


    $HOSTNAME does not contain the value you think it does.

    Do

    print "X $HOSTNAME X\n";

    And you'll see

    perldoc -f chomp



    --
    Posted via a free Usenet account from http://www.teranews.com
    Tintin, Oct 7, 2006
    #4
  5. kp

    Ch Lamprecht Guest

    kp wrote:
    > My perl module file includes this line:
    > $HOSTNAME = "/usr/bin/hostname";
    >
    > I have sourced this perl module file in my perl script.

    How did you do that?
    >
    > In my perl script I have an if loop :
    > if ( `$HOSTNAME` eq "rsd2" ) {

    `$ModulesPackage::HOSTNAME`
    > print "NFS server is fsd2";
    > }
    >
    >
    > However, this command substitution does not seem to work.
    >
    > Any clues???
    >
    > -kp
    >


    Should work like that:

    Chris@CHRIS ~
    $ perl -e'$hn = "hostname" ;print`$hn`'
    CHRIS

    The $HOSTNAME you want to use might be in another package??

    Christoph


    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Oct 7, 2006
    #5
  6. kp

    kp Guest

    Hi All,

    Thanks for all your posts.

    The problem here was that `$HOSTNAME` output had a newline character.

    I did the following for fixing this:
    $hostname = `$HOSTNAME`;
    chomp ($hostname);

    Then used the "$hostname" variable in the "if" loop.

    Thanks,
    Kiran


    kp wrote:
    > My perl module file includes this line:
    > $HOSTNAME = "/usr/bin/hostname";
    >
    > I have sourced this perl module file in my perl script.
    >
    > In my perl script I have an if loop :
    > if ( `$HOSTNAME` eq "rsd2" ) {
    > print "NFS server is fsd2";
    > }
    >
    >
    > However, this command substitution does not seem to work.
    >
    > Any clues???
    >
    > -kp
    kp, Oct 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tyson Marchuk
    Replies:
    7
    Views:
    129
    Brian McCauley
    Feb 24, 2004
  2. Geezer From The Freezer

    substitution from command line

    Geezer From The Freezer, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    83
    Tad McClellan
    Apr 24, 2004
  3. Replies:
    2
    Views:
    159
    Tad McClellan
    Mar 26, 2005
  4. ari
    Replies:
    3
    Views:
    84
  5. P B
    Replies:
    6
    Views:
    127
Loading...

Share This Page