Perl experts: I need to run shell command from perl using pipe
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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";
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.
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:
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:
For me, it is a little more readable. Again, other opinions may differ.
--- rod.
---------------
Quote:
Originally Posted by theNbomr
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:
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.
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);
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.
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
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
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";
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";
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'.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.