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
405 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 26

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 26

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 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 26

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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