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
396 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

8 Experts available now in Live!

Get 1:1 Help Now