Re-directing system call

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
Paul Johnston [ Do, 28 April 2005 12:26 ] [ ID #769047 ]

Re: Re-directing system call

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
Joe Smith [ Do, 28 April 2005 21:17 ] [ ID #769048 ]

Re: Re-directing system call

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 [ Do, 28 April 2005 23:52 ] [ ID #769049 ]

Re: Re-directing system call

Eric Teuber wrote:
> 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

and even shorter

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

Eric
Eric Teuber [ Fr, 29 April 2005 00:13 ] [ ID #769050 ]

Re: Re-directing system call

Eric Teuber wrote:
> 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

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
Eric Teuber [ Fr, 29 April 2005 00:27 ] [ ID #769051 ]
Perl » alt.perl » Re-directing system call

Vorheriges Thema: parsing user-entered content for rude words
Nächstes Thema: Perl Newbie