Command substitution in perl

K

kp

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
 
A

Ala Qumsieh

kp said:
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
 
J

Jürgen Exner

kp said:
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
 
T

Tintin

kp said:
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
 
C

Ch Lamprecht

kp said:
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
 
K

kp

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top