LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl experts: I need to run shell command from perl using pipe (https://www.linuxquestions.org/questions/programming-9/perl-experts-i-need-to-run-shell-command-from-perl-using-pipe-855089/)

idreesedu 01-08-2011 09:02 PM

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

Sergei Steshenko 01-08-2011 09:57 PM

Quote:

Originally Posted by idreesedu (Post 4217885)
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 ?

theNbomr 01-09-2011 11:49 AM

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

idreesedu 01-10-2011 02:37 PM

Quote:

Originally Posted by theNbomr (Post 4218470)
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.

theNbomr 01-10-2011 03:43 PM

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.

theNbomr 01-10-2011 03:55 PM

Quote:

Originally Posted by idreesedu (Post 4219653)
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.

idreesedu 01-10-2011 05:10 PM

Quote:

Originally Posted by theNbomr (Post 4219711)
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 (Post 4219711)
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.

idreesedu 01-10-2011 05:16 PM

Quote:

Originally Posted by theNbomr (Post 4219699)
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";

}
}}

-----

theNbomr 01-10-2011 06:11 PM

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.

idreesedu 01-10-2011 09:46 PM

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.

Tinkster 01-10-2011 10:59 PM

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

idreesedu 01-11-2011 09:00 AM

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";

Sergei Steshenko 01-11-2011 09:50 AM

Quote:

Originally Posted by idreesedu (Post 4220546)
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";
?

theNbomr 01-11-2011 10:39 AM

Quote:

Originally Posted by idreesedu (Post 4220546)
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.

idreesedu 01-11-2011 04:52 PM

Quote:

Originally Posted by theNbomr (Post 4220663)
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.


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