Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 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