Click to See Complete Forum and Search --> : Bad File Descriptors in Perl


Gotenks
02-01-2001, 02:48 PM
Greetings,

A while back I was working on a script which would read in every file in a directory one at a time, attach a header and footer around the text and spit out a new file. (It also took a few things from that file to create an index.html of all the files). The last time I ran this script (Thanks YarNess http://www.linuxnewbie.org/ubb/smile.gif) it had no problems whatsoever. Though, I'm now trying to run it on a directory of new text with modified headers (Dates, Volume numbers etc). The error I get goes like this...


[root@www v8n6]# ls
feedlot_footer.txt merger.pl v8n6pg13 v8n6pg20 v8n6pg30-31 v8n6pg38-39 v8n6pg46 v8n6pg60
feedlot_header.txt v8n6pg 5 v8n6pg14 v8n6pg22-23 v8n6pg32 v8n6pg40-41 v8n6pg48-49 v8n6pg63
feedlot_titlefooter.txt v8n6pg 6 v8n6pg16 v8n6pg24 v8n6pg34a v8n6pg42 v8n6pg52-53 v8n6pg66
feedlot_titleheader.txt v8n6pg 8-9 v8n6pg18 v8n6pg26-27 v8n6pg34b v8n6pg44 v8n6pg54-55
index.html v8n6pg12 v8n6pg18b v8n6pg28 v8n6pg35 v8n6pg45 v8n6pg58
[root@www v8n6]# ./merger.pl feedlot
Can't close feedlot_header.txt (Bad file descriptor)
[root@www v8n6]#


Of course, I've already checked all the permissions etc and can find no difference between this issue and the last In which it ran perfectly on. So, I tried ot run it on the past issue, Received the same error! http://www.linuxnewbie.org/ubb/frown.gif

At any rate, this note is getting a tad be large so if anyone has any ideas please lemme know. I'll post the script itself in the following post.

Thanks,
Gotenks

Gotenks
02-01-2001, 02:50 PM
First the script:


#!/usr/bin/perl -w
#
# Original code: Scott
# Modifications/Improvements: Brett & YaRness(LNO)

use strict;

if (!$ARGV[0]) {
print "\nFormat: ./merger.pl <client>\n";
exit;
}

# Read in command-line args here.
my $argument = "$ARGV[0]";
chomp $argument;

my $txtfile = "0";
my $txtfiledata = "0";

#Open up a fresh index and slap a header on it. (Only needed once per issue of course)
open (TITLES, ">index.html") | | die "Can't open index.html ($!)";
open (TITLEHEADER, "${argument}_titleheader.txt") | | die "Can't open ${argument}_titleheader.txt ($!)";
print TITLES <TITLEHEADER>;
close (TITLEHEADER) | | die "Can't close ${argument}_titleheader.txt ($!)";


#Lets run through the .article files in this directory
while (defined($txtfile = glob("*") ) ) {
if ($txtfile =~ /^.*\.article$/) {

#Open up the .txt article and get her ready
open (TXTFILE, "$txtfile") | | die "Can't open $txtfile ($!)";
my @txtfiledata = <TXTFILE>;
my $newtxtfile = $txtfile;
$newtxtfile =~ tr/A-Za-z0-9//cd;

#Open a new file (using the stripped $newtxtfile), insert the standard header.
open (HEADER, "${argument}_header.txt") | | die "Can't open ${argument}_header.txt ($!)";
open (NEWTXTFILE, ">new_$newtxtfile.html") | | die "Can't open new_$newtxtfile ($!)";
print NEWTXTFILE <HEADER>;
close (HEADER) | | die "Can't close ${argument}_header.txt ($!)\n";

#Lets spit the index listing into the new index.
print TITLES "<font face=\"arial, helvetica, sans-serif\"><a style =\"color:#000000; text-decoration:none\" href=\"./new_$newtxtfile.html\">";
print TITLES "$txtfiledata[0]";
print TITLES "</a></font><br>\n<img name =\"bar\" src=\"../../images/bar.gif\" width=\"341\" height=\"2\" border=\"0\"><br><br>";

#Copy line for line of data from old txt article to new html article.
foreach $txtfiledata (@txtfiledata) {
chomp $txtfiledata;
print NEWTXTFILE "$txtfiledata".'<br>'."\n";
}

#Lets close off this article with a footer
open (FOOTER, "${argument}_footer.txt") | | die "Can't open ${argument}_footer.txt ($!)";
print NEWTXTFILE <FOOTER>;
close (FOOTER) | | die "can't close ${argument}_footer.ext ($!)";

#Closeing the txt article, and the newly created html article.
close (TXTFILE) | | die "Can't close $txtfile ($!)";
close (NEWTXTFILE) | | die "Can't close new_$newtxtfile ($!)";
}
}

#Lets spit out the footer at the end of the index and wrap up this issue.
open (TITLEFOOTER, "${argument}_titlefooter.txt") | | die "Can't open ${argument}_titlefooter.txt ($!)";
print TITLES <TITLEFOOTER>;
close (TITLEFOOTER) | | die "Can't close ${argument}_titlefooter ($!)";

#Lets close up our global files.
close (TITLES) | | die "Can't close index.html ($!)";
close (HEADER) | | die "Can't close ${argument}_header.txt ($!)\n";
close (FOOTER) | | die "can't close ${argument}_footer.ext ($!)";



Now the header file:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<!--
Created: 20-Sep-00
By: Brett Phipps
Where:
-->
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=US-ASCII">
<META HTTP-EQUIV="Content-language" CONTENT="en-US">
<TITLE>Feed Lot Magazine Online, </TITLE>
<LINK REV=made href="maimaimailto:Phippsb@gcnet.com">
<META NAME="keywords" CONTENT="feed lot,magazine,online,cattle,feeder,information,high lights,articles,archive">
<META NAME="description" CONTENT="Feed Lot Magazine: Research Discovers that Cattle Overwhelmingly Prefer Forages Harvested in the Afternoon vs. Morning">
<META NAME="rating" CONTENT="General">
<META NAME="ROBOTS" CONTENT="ALL">
</HEAD>
<body bgcolor="#ffffff" text="#000000" link="#990000" vlink="#333333" alink="#0033cc">


<center>
<table border="0" cellpadding="0" cellspacing="0" width="586">
<tr>
<td align="left"><img src="../../images/logosmall.jpg" width="228" height="50" border="0"></td>
</tr>

<!--#FIRSTHEADER#-->
<tr valign="top">
<td colspan="2" align="center">
<table valign="top" cellspacing="0" cellpadding="0" border="0" width="100%" bgcolor="#000000"><tr>
<td align="left" valign="top"><font size="3" color="#FFFFFF" face="arial, helvetica, sans-serif"><b>Volume VIII Number 6</b></font></td>
<td align="left" valign="top"><form method="post" action="/cgi-bin/feedlot/htsearch">
<input type="hidden" name="config" value="htdig">
<input type="hidden" name="restrict" value="">
<input type="hidden" name="exclude" value="">
<input type="text" size="30" name="words" value="">
<input type="submit" value="Search">
</form>
</td>
<td align="right" valign="top"><font size="3" color="#FFFFFF" face="arial, helvetica, sans-serif"><b>November/December 2000</b></font></td>
</table>
</td>
</tr>
<!--#ENDFIRSTHEADER#-->

<!--#NAVIBAR#-->
<tr valign="top">
<td align="center">
<table width="100%"><tr>
<td align="center" bgcolor="#e60000"><font size="2" color="#FFFFFF" face="arial, helvetica, sans-serif"><b>
| <a style ="color:#ffffff; text-decoration:none" href="./">Back</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../search/">Search</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../staff.html">Staff</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../issues/">Archives</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../advertise.html">Advertising Info</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../contact.html">Contact Us</a>
| <a style ="color:#ffffff; text-decoration:none" href="../../">Home</a>
|
</b></font></td>
</table>
</td>
</tr>
<!--#ENDNAVIBAR-->


<tr>
<td valign="top">

<table width="100%">
<tr><td align="left">
<h3><font face="arial, helvetica, sans-serif" size="4">Research Discovers that Cattle Overwhelmingly Prefer Forages Harvested in the Afternoon vs. Morning

</font></h3>
</td>
<td align="right">
<font face="arial, helvetica, sans-serif" size="3"><i><b><!--#TITLEAUTHOR#--><br></b></i></font>
</td>
</tr>
</table>
<font face="arial, helvetica, sans-serif" size="3"><br>



Thanks again fellas! http://www.linuxnewbie.org/ubb/biggrin.gif

Gotenks

[This message has been edited by Gotenks (edited 01 February 2001).]

YaRness
02-01-2001, 03:04 PM
you are closing HEADER twice. the script doesn't need the "close (HEADER)..." at the end, it is trying to close a file that isn't opened.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

Gotenks
02-01-2001, 03:06 PM
Damn, I really need to reinstall MacPerl on this POS machine. Maybe that would keep me from missing mistakes that make me look like an idiot http://www.linuxnewbie.org/ubb/wink.gif.

Thanks Yar! http://www.linuxnewbie.org/ubb/biggrin.gif

Gotenks

YaRness
02-01-2001, 03:14 PM
hey why do you have "${argument}" instead of "$argument"?

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
02-01-2001, 03:35 PM
was bored, i made some modifications... eliminated a lot of filehandle opening and got rid of the $newtxtfile variable. also, all the "*.article" files HAVE to be only letters and numbers in the * part, but i think you prolly would want that anyway. could easily adjust the regexp to match more than A-Za-z0-9 though.

anyway, using some system calls instead of open/close stuff... some people don't like that, but i figure, this script is prolly gonna only be for unix, so may as well abuse it a little bit. i dunnno if this will work, i can't test it, but i think it will still work. look at it, see if you wanna try and adapt. the code is a little shorter, and a little easier to read.

also got rid of an extra variable in a foreach loop, and instead utilized the default $_ variable.

merger2.pl

#!/usr/bin/perl -w
#
# Original code: Scott
# Modifications/Improvements: Brett & YaRness(LNO)
use strict;
if (!$ARGV[0]) {
print "\nFormat: ./merger.pl <client>\n";
exit;
}
#
# Read in command-line args here.
my $argument = "$ARGV[0]";
chomp $argument;
my $txtfile = "0";
#
#Open up a fresh index and slap a header on it. (Only needed once per issue of course)
#
#YAR: i figure, may as well abuse system() and save one opening/closing of a file
#copy the header to index.html and then open index.html in append mode
system("cp ${argument}_titleheader.txt index.html");
#
open (TITLES, ">>index.html") or die "Can't open index.html ($!)";
#
#Lets run through the .article files in this directory
while (defined ($txtfile = glob("*") ) ) {
if ($txtfile =~ /^[A-Za-z0-9]+\.article$/) {

#
#YAR backticks instead of open/<>/close
my @txtfiledata = `cat $txtfile`
#
#Lets spit the index listing into the new index.
print TITLES "<font face=\"arial, helvetica, sans-serif\"><a style =\"color:#000000; text-decoration:none\" href=\"./new_$newtxtfile.html\">";
print TITLES "$txtfiledata[0]";
print TITLES "</a></font><br>\n<img name =\"bar\" src=\"../../images/bar.gif\" width=\"341\" height=\"2\" border=\"0\"><br><br>";
#
#YAR same modification as with the header file
system("cp ${argument}_header.txt new_$txtfile.html")
open (NEWTXTFILE, ">>new_$txtfile.html") or die "Can't open new_$txtfile ($!)";
#
#Copy line for line of data from old txt article to new html article.
foreach (@txtfiledata) {
chomp;
print NEWTXTFILE "$_".'<br>'."\n";
}
#YAR: let's abuse system calls some more, this time backticks instead
print NEWTXTFILE `cat ${argument}_footer.txt`;

}
}
#Lets spit out the footer at the end of the index and wrap up this issue.
#YAR and some more
print TITLES `cat ${argument}_titlefooter.txt`;
#
#Lets close up our global files.
close TITLES or die "Can't close index.html ($!)";



------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/