Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Below is a snippet of a simple script giving me problems. The first use of fgrep marked (1) redirects GREAT. The last 4 marked (2) simply output to the screen and ignore my pipe! If I change the $log to a constant it works fine? Any ideas?

#Scan the log dir for useful logfiles
chdir ("$BackPath");
(1) system "fgrep -l $Date * > /nwscan/tmplog";
chdir ("$MainPath");
$log1 = $target[0];
$log2 = $target1;
$log3 = $target2;
#print $log1;
#print $log2;
#print $log3;
chdir ("$BackPath");
(2)system "fgrep server $log1>>/nwscan/$Server";
(2)system "fgrep type $log1>>/nwscan/$Server";
(2)system "fgrep started $log1>>/nwscan/$Server";
(2)system "fgrep completion $log1>>/nwscan/$Server";

Replies are listed 'Best First'.
Re: redirect grep output
by Adam (Vicar) on Jan 12, 2001 at 22:40 UTC
    Any ideas?
    Yes.
    1. use strict;
    2. use warnings; or -w on Perl < 5.6
    3. check the sucess of system calls.
    4. use strict;
    5. use whitespace in your system calls.
    6. use <code> tags when posting code here
    7. use strict;
Re: redirect grep output
by chipmunk (Parson) on Jan 12, 2001 at 22:40 UTC
    You don't show where the value that ends up in $log1 comes from originally, but I'll bet you need to chomp it to remove a newline. If $log1 ends in a newline, then you're really passing two separate commands to system, an fgrep and a redirection of nothing.
      Thanks alot! A stupid mistake, but I am by no means a "Perl Monk". I chomped the var and it worked great!
      Kenny
Re: redirect grep output
by adamsj (Hermit) on Jan 12, 2001 at 22:26 UTC
    Have you considered defining $Server?

    Also, what is system returning when you make those calls? Have you checked that yet?