LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl script output can't be redirected to a file (https://www.linuxquestions.org/questions/programming-9/perl-script-output-cant-be-redirected-to-a-file-789989/)

nasridine 02-18-2010 12:48 PM

perl script output can't be redirected to a file
 
The perl script I wrote works fine if I print the result to screen
x0_amber.pl 1 1000 0 5

But whenever I want to output to a file with
x0_amber.pl 1 1000 0 5 > x0_out

it never really prints out to the file.

It worked earlier, but I was playing with my PATH lately, I don't know if it's related to that

Sergei Steshenko 02-18-2010 12:51 PM

Quote:

Originally Posted by nasridine (Post 3868444)
The perl script I wrote works fine if I print the result to screen
x0_amber.pl 1 1000 0 5

But whenever I want to output to a file with
x0_amber.pl 1 1000 0 5 > x0_out

it never really prints out to the file.

It worked earlier, but I was playing with my PATH lately, I don't know if it's related to that

So, what is the screen output ?

nasridine 02-18-2010 12:58 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3868450)
So, what is the screen output ?

Something like this:
frame 1-1000
0 1
0.1 1
0.2 1
0.3 0.98
0.4 0.97
0.5 0.96
....

The first line "frame 1-1000" should be printed out before any calculation is done.

nasridine 02-18-2010 01:35 PM

Quote:

Originally Posted by nasridine (Post 3868456)
Something like this:
frame 1-1000
0 1
0.1 1
0.2 1
0.3 0.98
0.4 0.97
0.5 0.96
....

The first line "frame 1-1000" should be printed out before any calculation is done.

Here is my code:


#!/usr/bin/perl


if ($#ARGV != 5)
{
die "Usage x0_amber traj_name frame1 frame2 r0 r1 dr\n";
}

$traj=$ARGV[0];
$frame1=$ARGV[1];
$frame2=$ARGV[2];
$r0=$ARGV[3];
$r1=$ARGV[4];
$dr=$ARGV[5];
print "frame $frame1-$frame2\n";

$no_frm=$frame2-$frame1+1;
for($r=$r0;$r<=$r1;$r=$r+$dr)
{
for($i=$frame1;$i<=$frame2;$i++)
{
open(PDB,"$traj.$i");
@line_pdb=<PDB>;
close PDB;
}
$x0=$n_count/$no_frm;
print "$r $x0\n";
}


If I comment out the content in the inner loop, it prints out to a file ok. However if I uncomment these three lines, it just can't print to a file.

pixellany 02-18-2010 01:46 PM

When you run an executable file in BASH, the process of redirection is normally (I'm pretty sure) independent of what is inside the executable file. The exception I suppose is if the executable does something that overrides the re-direction.

If it does not output to a file, where DOES the output go?

Sergei Steshenko 02-18-2010 01:54 PM

Quote:

Originally Posted by nasridine (Post 3868488)
Here is my code:


#!/usr/bin/perl


if ($#ARGV != 5)
{
die "Usage x0_amber traj_name frame1 frame2 r0 r1 dr\n";
}

$traj=$ARGV[0];
$frame1=$ARGV[1];
$frame2=$ARGV[2];
$r0=$ARGV[3];
$r1=$ARGV[4];
$dr=$ARGV[5];
print "frame $frame1-$frame2\n";

$no_frm=$frame2-$frame1+1;
for($r=$r0;$r<=$r1;$r=$r+$dr)
{
for($i=$frame1;$i<=$frame2;$i++)
{
open(PDB,"$traj.$i");
@line_pdb=<PDB>;
close PDB;
}
$x0=$n_count/$no_frm;
print "$r $x0\n";
}


If I comment out the content in the inner loop, it prints out to a file ok. However if I uncomment these three lines, it just can't print to a file.

What does "it just can't print to a file" mean ? What are the contents of the file you try to redirect to ?

chrism01 02-18-2010 09:59 PM

1. use the following
Code:

#!/usr/bin/perl -w
use strict;

2. This loop does nothing
Code:

for($i=$frame1;$i<=$frame2;$i++)
{
    open(PDB,"$traj.$i");
    @line_pdb=<PDB>;
    close PDB;
}

ie the array @line_pdb is not used anywhere

3. This var is not defined/set anywhere: $n_count : so the calc & print will not do what you want.

nasridine 02-19-2010 12:13 AM

Quote:

Originally Posted by chrism01 (Post 3868925)
1. use the following
Code:

#!/usr/bin/perl -w
use strict;

2. This loop does nothing
Code:

for($i=$frame1;$i<=$frame2;$i++)
{
    open(PDB,"$traj.$i");
    @line_pdb=<PDB>;
    close PDB;
}

ie the array @line_pdb is not used anywhere

3. This var is not defined/set anywhere: $n_count : so the calc & print will not do what you want.

I just found out, it is not that my script was not able to print out to a file, it is because of the buffering. Perl only prints out once at the end or when buffer is full


All times are GMT -5. The time now is 03:37 AM.