Re: Syntax query



On Wed, Apr 15, 2009 at 10:41, John W. Krahn <jwkrahn@xxxxxxx> wrote:
Chas. Owens wrote:

On Wed, Apr 15, 2009 at 09:20, Rajini Naidu <rajinidots@xxxxxxxxx> wrote:

 I am trying to execute the below line in the perl program and returns a
value 0.

my $test2 = "";
my $include =  "file";

$test2 = system("/usr/atria/bin/cleartool desc \$include | grep created |
awk -F\" \" \'{print \$2}\' | cut -b 1-9 ");

The value returned by $test2 is 0. I suspect grep and awk commands are
not
getting executed.
Is there any thing wrong in the syntax ????

The system[1] function returns 0 on success and a nonzero value
specific to the operating system on failure (often the programs
exit code and the value returned by exec[2]).  You could use the
qx// operator[3] to capture the STDOUT of the command:

my $test2 = qx{/usr/atria/bin/cleartool desc \$include |
   grep created | awk -F\" \" \'{print \$2}\' | cut -b 1-9};

but a much safer and faster way is to use the open[4] function
and avoid using external programs like grep and awk:

open my $pipe, "-|", "/usr/atria/bin/cleartool", "desc", $include
   or die "could not run cleartool: $!";

my @matches;
while (<$pipe>) {
   next unless /created/;
   my @rec = split;
   push @matches, substr $rec[1], 0, 9;
}

Don't forget to verify that the pipe also closed correctly:

close OUTPUT or warn $! ? "Error closing /usr/atria/bin/cleartool pipe: $!"
                       : "Exit status $? from /usr/atria/bin/cleartool";
snip

If that is going to match the example it should be

close $pipe
or warn $! ? "Error closing /usr/atria/bin/cleartool pipe: $!"
: "Exit status $? from /usr/atria/bin/cleartool";


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.



Relevant Pages

  • Re: Syntax query
    ... I am trying to execute the below line in the perl program and returns ... The value returned by $test2 is 0. ... I suspect grep and awk commands are ...
    (perl.beginners)
  • Re: Syntax query
    ... I'd suggest placing your pipeline of commands into a bash shell script and running it manually from the command line and carefully observing it running. ... I am trying to execute the below line in the perl program and returns ... I suspect grep and awk commands are ...
    (perl.beginners)
  • Re: Optimax
    ... Instead of calling 'system' to execute ... pipe and executes them. ... # Send the command to the background and wait for it to complete. ... # execution of the optimars under different os's. ...
    (rec.games.corewar)
  • Re: Syntax query
    ... I suspect grep and awk commands are not ... my @rec = split; ... Or if you just want the first match: ...
    (perl.beginners)
  • Re: [MSH] Requires an executable on the RHS of a pipe?
    ... Monad Program Management ... If I attempt to pipe to a WSH script without specifying the script ... MSH C:\> get-content Test.js ... Program 'Test.js' failed to execute: The specified executable is not a ...
    (microsoft.public.windows.server.scripting)