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
399 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now