LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2010, 12:48 PM   #1
nasridine
Member
 
Registered: Jan 2010
Posts: 52

Rep: Reputation: 15
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
 
Old 02-18-2010, 12:51 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by nasridine View Post
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 ?
 
Old 02-18-2010, 12:58 PM   #3
nasridine
Member
 
Registered: Jan 2010
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 02-18-2010, 01:35 PM   #4
nasridine
Member
 
Registered: Jan 2010
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nasridine View Post
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.

Last edited by nasridine; 02-18-2010 at 01:37 PM.
 
Old 02-18-2010, 01:46 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 02-18-2010, 01:54 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by nasridine View Post
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 ?
 
Old 02-18-2010, 09:59 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 02-19-2010, 12:13 AM   #8
nasridine
Member
 
Registered: Jan 2010
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl DBI script - No output from program nanda22 Linux - Newbie 2 09-02-2009 04:53 AM
Apparent File Size Limit for redirected standard output Frank M Salter Slackware 1 08-22-2009 07:53 AM
LXer: Managing Swatch Output With Yet Another Perl Script LXer Syndicated Linux News 0 12-08-2008 05:41 AM
How to access a redirected output file from within a script adelie Linux - Newbie 5 05-15-2008 04:40 PM
can output of tail be redirected? dsids Linux - Newbie 19 10-16-2006 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration