For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > May 2005 > Re-directing system call









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re-directing system call
Paul Johnston

2005-04-28, 8:56 am

Hi
I have a program which makes a system call as below:

<snip>
$run_this_first = "adfind -b dc=co,dc=umist,dc=ac,dc=uk -h $ARGV[0] -f
(cn=$ARGV[2])";
system $run_this_first ;
</snip>


This sends a stream of output to the stdio (screen)
How can I redirect this to a file ?
I have a similar section called run_this_second which need to be sent
to a different file so I cannot simply >> the whole output

I have opened a file for writing
open (FIRST,">e:/ad_stuff/first_file");
And I can write to this with :
print FIRST "hello world to first";

But I cannot get the system call to write to this file !


Any suggestions ?

TIA Paul
Joe Smith

2005-05-01, 8:56 pm

Paul Johnston wrote:

> system $run_this_first ;
>
> This sends a stream of output to the stdio (screen)
> How can I redirect this to a file ?


You need to learn two things.
1) The system() function accepts Bourne Shell (/bin/sh) syntax.
2) Use backticks (`` or qx{}) to grab output from a program.

system "$run_this_first >output_file";
or
$_ = `$run_this_first`; print FILE $_;

-Joe
Eric Teuber

2005-05-01, 8:56 pm

Paul Johnston wrote:
> Hi
> I have a program which makes a system call as below:
>
> <snip>
> $run_this_first = "adfind -b dc=co,dc=umist,dc=ac,dc=uk -h $ARGV[0] -f
> (cn=$ARGV[2])";
> system $run_this_first ;
> </snip>
>
>
> This sends a stream of output to the stdio (screen)
> How can I redirect this to a file ?
> I have a similar section called run_this_second which need to be sent
> to a different file so I cannot simply >> the whole output
>
> I have opened a file for writing
> open (FIRST,">e:/ad_stuff/first_file");
> And I can write to this with :
> print FIRST "hello world to first";
>
> But I cannot get the system call to write to this file !
>
>
> Any suggestions ?
>
> TIA Paul


print FIRST $_ foreach `$run_this_first`;

Eric
Eric Teuber

2005-05-01, 8:56 pm

Eric Teuber wrote:
> Paul Johnston wrote:
>
>
>
> print FIRST $_ foreach `$run_this_first`;
>
> Eric


and even shorter

not tested:
foreach $i ("first", "second") { print uc($i) $_ foreach `run_this_$i`};

Eric
Eric Teuber

2005-05-01, 8:56 pm

Eric Teuber wrote:
> Paul Johnston wrote:
>
>
>
> print FIRST $_ foreach `$run_this_first`;
>
> Eric


and even shorter if you want to redirect both system calls to different
logfiles.

not tested:
foreach $i ("first", "second") { print uc($i) $_ foreach `$run_this_$i`};

Eric
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com