Variable substitution

S

sebastien.godier

Hello,

My Perl script is first reading a text file containing a list of
computer names I want to run a command against.
The name of each computer is stored inside $_ variable.

So, I'm trying to generate my external command using this variable
inside a loop. Like this

system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

The problem is that $_ is not substituted at all. If I replace $_ by a
real computer name, it works. But I don't know how to declare $_ as a
variable in this line.

Any idea welcome!

Thanks
Sebastien
 
M

Mirco Wahab

So, I'm trying to generate my external command using this variable
inside a loop. Like this

system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

The problem is that $_ is not substituted at all. If I replace $_ by a
real computer name, it works. But I don't know how to declare $_ as a
variable in this line.

Show your loop!

Regards

M.
 
S

Seb

Ok so here my complete script:


##############
#!C:\Perl\bin\perl.exe -w
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;

#############
 
A

anno4000

Seb said:
Ok so here my complete script:

[snipped except]
system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno
 
S

Seb

(e-mail address removed)-berlin.de a écrit :
Seb said:
Ok so here my complete script:

[snipped except]
system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

##########

So to answer you Anno, this typo was not the source of my problem. This
is the script causing me problem. $_ is not getting subsituted
correctly. But replacing $_ by a real computer name works fine.

Sebastien
 
A

anno4000

Seb said:
(e-mail address removed)-berlin.de a écrit :
Seb said:
Ok so here my complete script:

[snipped except]
system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

Oh boy... Please take some more care with your postings.
###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

##########

So to answer you Anno, this typo was not the source of my problem.

Too bad you wasted everybody's time with it.
This
is the script causing me problem. $_ is not getting subsituted
correctly. But replacing $_ by a real computer name works fine.

How do you know $_ is not substituted? Have you printed the string
instead of giving it to system(), so you can see what's actually in
there?

I have done so after making runnable code from your text. I made $_
contain "xxxxxxxxx" and it printed

REG QUERY "\\xxxxxxxxx\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"
/v "Start Page

on a single line. How is that not what you would expect?

Anno
 
S

Seb

(e-mail address removed)-berlin.de a écrit :
Seb said:
(e-mail address removed)-berlin.de a écrit :
Ok so here my complete script:

[snipped except]

system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

Oh boy... Please take some more care with your postings.
###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

##########

So to answer you Anno, this typo was not the source of my problem.

Too bad you wasted everybody's time with it.
This
is the script causing me problem. $_ is not getting subsituted
correctly. But replacing $_ by a real computer name works fine.

How do you know $_ is not substituted? Have you printed the string
instead of giving it to system(), so you can see what's actually in
there?

I have done so after making runnable code from your text. I made $_
contain "xxxxxxxxx" and it printed

REG QUERY "\\xxxxxxxxx\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"
/v "Start Page

on a single line. How is that not what you would expect?

Anno


You put me on the way !! The problem comes from the new line character.
In other word, the REG QUERY line is splitted like this due to this
character:

REG QUERY "\\xxxxxxxxx
\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"

So, I must remove this character from $_. Trying with chomp($_) is not
making it. But I think I can succeed using a temporar variable.

Sebastien
 
S

Seb

(e-mail address removed)-berlin.de a écrit :
Seb said:
(e-mail address removed)-berlin.de a écrit :
Ok so here my complete script:

[snipped except]

system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

Oh boy... Please take some more care with your postings.
###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

##########

So to answer you Anno, this typo was not the source of my problem.

Too bad you wasted everybody's time with it.
This
is the script causing me problem. $_ is not getting subsituted
correctly. But replacing $_ by a real computer name works fine.

How do you know $_ is not substituted? Have you printed the string
instead of giving it to system(), so you can see what's actually in
there?

I have done so after making runnable code from your text. I made $_
contain "xxxxxxxxx" and it printed

REG QUERY "\\xxxxxxxxx\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"
/v "Start Page

on a single line. How is that not what you would expect?

Anno


You put me on the way !! The problem comes from the new line character
at the end of $_ variable. In other word, the REG QUERY line is
splitted like this due to this character:

REG QUERY "\\xxxxxxxxx
\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"

So, I must remove this character from $_. Trying with chomp($_) is not
making it. But I think I can succeed using a temporar variable.

Sebastien
 
S

Seb

Seb a écrit :
(e-mail address removed)-berlin.de a écrit :
Seb said:
(e-mail address removed)-berlin.de a écrit :
Ok so here my complete script:

[snipped except]

system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet

The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

Oh boy... Please take some more care with your postings.
###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
system ("REG QUERY \"\\\\$_\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
Explorer\\Main\" /v \"Start Page");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

##########

So to answer you Anno, this typo was not the source of my problem.

Too bad you wasted everybody's time with it.
This
is the script causing me problem. $_ is not getting subsituted
correctly. But replacing $_ by a real computer name works fine.

How do you know $_ is not substituted? Have you printed the string
instead of giving it to system(), so you can see what's actually in
there?

I have done so after making runnable code from your text. I made $_
contain "xxxxxxxxx" and it printed

REG QUERY "\\xxxxxxxxx\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"
/v "Start Page

on a single line. How is that not what you would expect?

Anno


You put me on the way !! The problem comes from the new line character
at the end of $_ variable. In other word, the REG QUERY line is
splitted like this due to this character:

REG QUERY "\\xxxxxxxxx
\HKLM\SOFTWARE\MICROSOFT\Internet Explorer\Main"

So, I must remove this character from $_. Trying with chomp($_) is not
making it. But I think I can succeed using a temporar variable.

Sebastien


Yeah, here is a working version

######
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {
$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file.
print OUTPUTFILE "$_";
# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n\t\t\t $_ FAILED.";
}
else
{
# The computer is on the network. Let's run the commands against it.
open(DEBUGCOMMANDS,">>debug-commands.txt");
print DEBUGCOMMANDS "$_\n";
$computer = $_;
$computer =~ s/\s+$//;
system ("REG QUERY
\"\\\\$computer\\HKLM\\SOFTWARE\\MICROSOFT\\Internet Explorer\\Main\"
/v \"Start Page\"");

# chomp remove the \n from $_ variable to print it correctly to
STDOUT
chomp($_);
print "\n$_ successfull.";
}
print DEBUGPING $debugping;
}

######

Many thanks Anno !!
Sebastien
 
G

Guest

(e-mail address removed)-berlin.de a écrit :
Seb said:
Ok so here my complete script:
[snipped except]
system ("REG QUERY \"\\\\$cpname\\HKLM\\SOFTWARE\\MICROSOFT\\Internet
The variable $cpname you try to interpolate is nowhere defined. If
you had switched on warnings, Perl would have told you so.

Change $cpname to $_ and it will work. And, please, before you
ask hundreds of people for help, take *one* close look at your code.

Anno

Will searching the solution, I realized that I posted a wrong version
of my script. I deleted it and now here is the good one.

###############
#!C:\Perl\bin\perl.exe
#

# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");

# When a computer is unavailable, write it name to the log file.
open(OUTPUTFILE,">errors.txt");

# For debugging purpose
open(DEBUGPING,">debug-ping.txt");
open(DEBUGCOMMANDS,">debug-commands.txt");

system("cls");
while (<INPUTFILE>) {

Print out $_ before you try to ping it. I suspect that it has a newline
sequence on the end of it.

$debugping = `ping -n 1 $_ 2>&1`;
if ( $? >> 8 == 1 )
{
# That means the computers is unreachable. Exporting the name of such
computers to the log file. [...]
 
T

Tad McClellan

Seb said:
#!C:\Perl\bin\perl.exe -w
# Read the list of computer to run the script against
open(INPUTFILE,"computers.txt") || die ("File $INPUTFILE does not
exist.");


You do not read anything there, so the comment is misleading.

You should not ignore warning messages.

If the file does not exist, your error message will say:

File does not exist. at ...

If the file does exist, but fails for some other reason
(eg. permission denied), your error message will say:

File does not exist. at ...

You should let perl give the reason why instead of guessing one
particlar reason on your own:

my $fname = 'computers.txt';
open(INPUTFILE, $fname) || die ("File '$fname' does not exist: $!");
^^
^^
open(OUTPUTFILE,">errors.txt");


You should always, yes *always*, check the return value from open().

print OUTPUTFILE "$_";


print OUTPUTFILE $_;
or
print OUTPUTFILE;


See:

perldoc -q vars

What’s wrong with always quoting "$vars"?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top