Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I am trying to assign an iutput of perl command to a variable . but getting an erro. below is the pelr command.

Posted on 2011-03-25
6
Medium Priority
?
426 Views
Last Modified: 2013-11-13

#!/usr/sbin/perl
value=`perl -ne '{/zone "rajat.com" {/ and print "$."}' /etc/postfix/test | head -1`
print  $value;

When i run this script i get the below error
Error-
Can't modify constant item in scalar assignment at /usr/local/sbin/test25 line 78, near "print"
syntax error at /usr/local/sbin/test25 line 78, near "print"
Execution of /usr/local/sbin/test25 aborted due to compilation errors.
0
Comment
Question by:pehalwaan
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:wilcoxon
ID: 35217937
It looks like you're missing a semi-colon at the end of the assignment line.
#!/usr/sbin/perl
value=`perl -ne '{/zone "rajat.com" {/ and print "$."}' /etc/postfix/test | head -1`;
print  $value;

Open in new window

0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 35217982
Rather than having a perl script call a perl one-liner, why not do this?  It will give the same output.
#!/usr/sbin/perl

open IN, '/etc/postfix/test' or die "could not open /etc/postfix/text: $!";
while (<IN>) {
    next unless /zone "rajat.com" {/;
    print $.;
    last;
}
close IN;

Open in new window

0
 
LVL 3

Author Comment

by:pehalwaan
ID: 35218829
I want to use one liner only and want to store the output of that one liner into the variable so that i can use that value to start a loop.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 35219245
I did notice both your original code and my first code have a typo - it should be "$value=..." in the first line.

Calling a one-liner will work but will be much less efficient.  If you want to use the return to start a loop, you can do...
#!/usr/sbin/perl

# you should always use strict and warnings...
use strict;
use warnings;

my $start;
open IN, '/etc/postfix/test' or die "could not open /etc/postfix/text: $!";
while (<IN>) {
    next unless /zone "rajat.com" {/;
    $start = $.;
    last;
}
close IN;

for ($start..999) {
}

Open in new window

0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 35219251
So, if you really want to use a one-liner, this is the further corrected code...
#!/usr/sbin/perl
$value=`perl -ne '{/zone "rajat.com" {/ and print "$."}' /etc/postfix/test | head -1`;
print  $value;

Open in new window

0
 
LVL 3

Author Closing Comment

by:pehalwaan
ID: 35219324
Thanks a lot.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question