LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2011, 09:02 PM   #1
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Rep: Reputation: 0
Perl experts: I need to run shell command from perl using pipe


Hello experts attention need

I am looking to run a unix shell command in perl script but its getting failing due to pipe "|" and semi colon ";"
can any one help me to run folloing command in perl script
p4 -o $valu | sed -e "s/<xxxx>/zzz/g/\;/s/<ggg>/ffff/g" | p4 change -i
above command should run in one shot

THANKS IN ADVANCE
 
Old 01-08-2011, 09:57 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by idreesedu View Post
Hello experts attention need

I am looking to run a unix shell command in perl script but its getting failing due to pipe "|" and semi colon ";"
can any one help me to run folloing command in perl script
p4 -o $valu | sed -e "s/<xxxx>/zzz/g/\;/s/<ggg>/ffff/g" | p4 change -i
above command should run in one shot

THANKS IN ADVANCE
You didn't post your Perl code, nor you published the exact error messages.

Also, did you use

Code:
use strict;
use warnings;
at the top of your Perl script ?
 
Old 01-09-2011, 11:49 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I agree with Sergei. But the real question is why you think it is useful to use perl to launch a shell script that uses sed. Anything sed can do, perl itself can do at least as well.
You can use at least three common methods to run a shell script: backticks, system(), or opened() as a pipe.
--- rod
 
Old 01-10-2011, 02:37 PM   #4
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
I agree with Sergei. But the real question is why you think it is useful to use perl to launch a shell script that uses sed. Anything sed can do, perl itself can do at least as well.
You can use at least three common methods to run a shell script: backticks, system(), or opened() as a pipe.
--- rod

why you think it is useful to use perl to launch a shell script that uses sed???
I am using sed because I was using perforce tool, for this tool the command should chanage descript bye pipe and it replaces the description.
p4 -o $valu --> Opening changelist
sed -e "s/<xxxx>/zzz/g/\;/s/<ggg>/ffff/g" --> replacing string to opened file
p4 change -i --> saving the file what ever descript we given

let me know if is there is any other I can do this in perl.

I am not an expert in scripting show I am working on Perl its all most done as I mestioned it was failing due to the command after running the scrip I getting following errors.

after running script getting folloing errors
Global symbol "$thirdvalue" requires explicit package name at Integration.pl line 33.
syntax error at Integration.pl line 33, near ""p4 -c change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g"
Integration.pl has too many errors.

line 33 code --> my $fifthCommand = "p4 -c change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g" | p4 change -i";


Thanks for your time and efforts.
 
Old 01-10-2011, 03:43 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You seem to be using a variable '$thirdvalue', without any package declaration. It seems to be used as an argument to a command, so it is probably also undefined. Without knowing anything about the logic of your code, my guess is that somewhere earlier in the script, there should probably an assignment to that variable, for instance:
Code:
my $thirdvalue = "whatever";
Or, just a guess; perhaps it is a typo, and should have been '$thirdValue'. In any case, the error message you posted has nothing to do with launching a shell script.
It may be mainly a matter of personal preference, but I would still not launch sed to do what Perl can already do by itself. Others will no doubt make arguments to the contrary.
By the way, your post would be more readable if code was posted in code tags.

--- rod.

Last edited by theNbomr; 01-10-2011 at 03:48 PM.
 
Old 01-10-2011, 03:55 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by idreesedu View Post
let me know if is there is any other I can do this in perl.
Others may argue that this is no better, and since you need to launch another program anyway, it would be a valid argument. Still, and for the sake of demonstration, something such as this (untested) keeps the job in the minimum number of tools:
Code:
    open( PERFORCE, "p4 -c change -o $thirdvalue |" );
    my @perforceResult = <PERFORCE>;
    close PERFORCE;

    foreach my $i ( 0 .. (scalar @perforceResult) - 1){
        $perforceResult[$i] ~= s/<enter description>/Test Merge/g;
        $perforceResult[$i] ~= s/<enter effort>/1/g;
    }

    open( PERFORCE "| p4 change -i" );
    print PERFORCE @perforceResult;
    close PERFORCE;
For me, it is a little more readable. Again, other opinions may differ.
--- rod.

Last edited by theNbomr; 01-10-2011 at 03:56 PM.
 
Old 01-10-2011, 05:10 PM   #7
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Others may argue that this is no better, and since you need to launch another program anyway, it would be a valid argument. Still, and for the sake of demonstration, something such as this (untested) keeps the job in the minimum number of tools:
Code:
    open( PERFORCE, "p4 -c change -o $thirdvalue |" );
    my @perforceResult = <PERFORCE>;
    close PERFORCE;

    foreach my $i ( 0 .. (scalar @perforceResult) - 1){
        $perforceResult[$i] ~= s/<enter description>/Test Merge/g;
        $perforceResult[$i] ~= s/<enter effort>/1/g;
    }

    open( PERFORCE "| p4 change -i" );
    print PERFORCE @perforceResult;
    close PERFORCE;
For me, it is a little more readable. Again, other opinions may differ.
--- rod.
---------------

Quote:
Originally Posted by theNbomr View Post
Others may argue that this is no better, and since you need to launch another program anyway, it would be a valid argument. Still, and for the sake of demonstration, something such as this (untested) keeps the job in the minimum number of tools:
Code:
    open( PERFORCE, "p4 -c change -o $thirdvalue |" );
    my @perforceResult = <PERFORCE>;
    close PERFORCE;

    foreach my $i ( 0 .. (scalar @perforceResult) - 1){
        $perforceResult[$i] ~= s/<enter description>/Test Merge/g;
        $perforceResult[$i] ~= s/<enter effort>/1/g;
    }

    open( PERFORCE "| p4 change -i" );
    print PERFORCE @perforceResult;
    close PERFORCE;
For me, it is a little more readable. Again, other opinions may differ.
--- rod.
I did try to run your code and its still giving me errors.
after running script code following errors occured.
syntax error at Integration.pl line 37, near "] ~"
syntax error at Integration.pl line 38, near "] ~"
Missing comma after first argument to open function at Integration.pl line 41, near ""| p4 change -i" )"
Execution of Integration.pl aborted due to compilation errors.
-------
code on line 37, 38 and 41.
37. foreach my $i ( 0 .. (scalar @perforceResult) - 1){
38. $perforceResult[$i] ~= s/<enter description>/Test Merge/g;
39. $perforceResult[$i] ~= s/<enter effort>/1/g;
40. }
41. open( PERFORCE "| p4 change -i" );
42. print PERFORCE @perforceResult;
43. close PERFORCE;


Let me know if you anything needs to change, as I am not into programming so I might doing somthing wrong.
 
Old 01-10-2011, 05:16 PM   #8
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
You seem to be using a variable '$thirdvalue', without any package declaration. It seems to be used as an argument to a command, so it is probably also undefined. Without knowing anything about the logic of your code, my guess is that somewhere earlier in the script, there should probably an assignment to that variable, for instance:
Code:
my $thirdvalue = "whatever";
Or, just a guess; perhaps it is a typo, and should have been '$thirdValue'. In any case, the error message you posted has nothing to do with launching a shell script.
It may be mainly a matter of personal preference, but I would still not launch sed to do what Perl can already do by itself. Others will no doubt make arguments to the contrary.
By the way, your post would be more readable if code was posted in code tags.

--- rod.
$thirdValue have been difined folloing is the perl code.

#!/usr/bin/perl
use strict;
use warnings;

#Get the workflow log
my $logfile ="/home/ocd/msc/auto/integration/code/log.log";
open LOG, ">>$logfile" or die $!;
print LOG "starting of the script \n";
my $list = "/home/ocd/msc/auto/integration/code/list.txt";
print LOG "$list";
open(DAT, $list) || die("Could not open file!");
foreach $line (<DAT>) {
if($line){
print LOG "$line\n";
$my_string= $line;#chomp($line);
my @values = split(' ', $my_string);

$firstvalue=$values[0];
$secondvalue=$values[1];
$thirdvalue=$values[2];

print "$firstvalue\n";
print "$secondvalue\n";
print "$thirdvalue\n";

my $fifthCommand = "p4 -c iwork change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g" | p4 change -i";
my $secondCommand = "p4 -c iwork integrate -c $thirdvalue -o -f $firstvalue $secondvalue";
my $thirdCommand = "p4 -c iwork resolve -at";
my $fourthCommand = "p4 -c iwork submit -c $thirdvalue";

#print "1-$firstCommand \n";
print "2-$fifthCommand \n";
print "3-$secondCommand \n";
print "4-$thirdCommand \n";
print "5-$fourthCommand \n";

#my $error = "$? \n";
print LOG $dataResult;
print LOG "error:$error";
if($error!=0)
{
print LOG "$error \n";

}
}}

-----
 
Old 01-10-2011, 06:11 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
my $firstvalue=$values[0];
my $secondvalue=$values[1];
my $thirdvalue=$values[2];
I may be overlooking something in my own code, but the only thing I can see is the missing comma as reported by the Perl compiler. The other errors you report must stem from code previous to what I posted.
Code:
    open( PERFORCE, "| p4 change -i" );
    print PERFORCE @perforceResult;
    close PERFORCE;
Please post your source code in code tags to make it readable.
--- rod.
 
Old 01-10-2011, 09:46 PM   #10
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Rod,

Yes the comma error has been resloved but still from your code I'm getting following error, Im postion errors and code at line# 37 & 38.

I honestly apprecieate your help, since Iam 000 at coding.
Thanks in advace.

Eeorr
syntax error at Integration.pl line 37, near "] ~"
syntax error at Integration.pl line 38, near "] ~"
Execution of Integration.pl aborted due to compilation errors.
---
code at line 37 & 38

$perforceResult[$i] ~= s/<enter description>/Test Merge/g;
$perforceResult[$i] ~= s/<enter effort>/1/g;

if you want you can take a look at my compete code in this postings.
 
Old 01-10-2011, 10:59 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Eeorr
syntax error at Integration.pl line 37, near "] ~"
syntax error at Integration.pl line 38, near "] ~"
Execution of Integration.pl aborted due to compilation errors.
---
code at line 37 & 38

$perforceResult[$i] ~= s/<enter description>/Test Merge/g;
$perforceResult[$i] ~= s/<enter effort>/1/g;

if you want you can take a look at my compete code in this postings.
That'll be because the operator isn't ~=, but =~




Cheers,
Tink
 
Old 01-11-2011, 09:00 AM   #12
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
That'll be because the operator isn't ~=, but =~
after making changes to the syntax I am not getting any erorrs but the command failing because of input and output of command.
Error after running script
Unknown command. Try 'p4 help' for info.
Error in change specification.
Missing required field 'Change'.

my original command to be run
my $fifthCommand = "p4 -c change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g" | p4 change -i";
 
Old 01-11-2011, 09:50 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by idreesedu View Post
That'll be because the operator isn't ~=, but =~
after making changes to the syntax I am not getting any erorrs but the command failing because of input and output of command.
Error after running script
Unknown command. Try 'p4 help' for info.
Error in change specification.
Missing required field 'Change'.

my original command to be run
my $fifthCommand = "p4 -c change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g" | p4 change -i";
Before calling 'system' insert

Code:
warn "\$fifthCommand=$fifthCommand";
in order to be able to see actual full command to be executed by 'system'.
...
Don't you need to escape double quotes ? I.e. shouldn't your command actually be:

Code:
my $fifthCommand = "p4 -c change -o $thirdvalue | sed -e \"s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g\" | p4 change -i";
?
 
Old 01-11-2011, 10:39 AM   #14
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by idreesedu View Post
That'll be because the operator isn't ~=, but =~
after making changes to the syntax I am not getting any erorrs but the command failing because of input and output of command.
Error after running script
Unknown command. Try 'p4 help' for info.
Error in change specification.
Missing required field 'Change'.

my original command to be run
my $fifthCommand = "p4 -c change -o $thirdvalue | sed -e "s/<enter description>/Test Merge/g/\;/s/<enter effort>/1/g" | p4 change -i";
Thanks to Tinkster for catching the syntax error. I stared at the code for a long time, and couldn't see that. I vow to never post untested code again. And never drink too much beer.

It looks like the argument to the 'p4' command is not 'change' but is 'Change'.

--- rod.
 
Old 01-11-2011, 04:52 PM   #15
idreesedu
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by theNbomr View Post
Thanks to Tinkster for catching the syntax error. I stared at the code for a long time, and couldn't see that. I vow to never post untested code again. And never drink too much beer.

It looks like the argument to the 'p4' command is not 'change' but is 'Change'.

--- rod.
my issue has been fixed, what I did is I have break dwon the command in two steps so it started working see below how I breaked it.

my $firstCommand = "p4 change -o $thirdvalue | sed -e 's/<enter description>/Test Merge/g' | p4 change -i";
my $firstCommand1 = "p4 change -o $thirdvalue | sed -e 's/<enter effort>/1/g' | p4 change -i";

Thank you so much all the time you have spend to help me, I really appreciate.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
32512 error code in Perl when trying to run shell command basher400 Programming 2 11-29-2010 09:18 AM
run shell script from perl kzcom Linux - Networking 4 09-04-2010 07:30 AM
running shell command in Perl script deci007 Linux - Newbie 5 02-07-2010 06:12 PM
what is wrong with the shell/perl command xterm..- alix123 Programming 1 08-14-2005 08:06 PM
perl experts RHlinux9 Programming 1 12-08-2003 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration