Tugger the SLUGger!SLUG Mailing List Archives

[SLUG] Perl script


Hi
i have a big hard drive on my debian box where i store mainly software and a
few tunes. what i would like to do is create an index.html file for each
directory and subdirectory. What I don?t have is the scripting skills to do
this. I reckon theres probably a Perl script (I?d like to do it with Perl so
I can learn a bit about it) to do this somewhere out there already. Can
someone point me in the right direction or, even better donate some time to
holding my hand as I learn to do it myself (which might take a while as the
last time I did any programming was way back in the late 80s with Pascal and
Basic)   ;s

I have attached two scripts - one bash and one perl. They sorta do the things that you are thinking of and will provide some starting point perhaps.
Oh I am not a Perl programmer by profession so my code has no guarantees.

Usage of mkbinindex:
Im my ~/bin dir I have lots of files. They all are like this:

#!/usr/bin/perl -w
# Makes list of all bin directories, files and their comments.
# By "comment" I mean the first line after #!/bin/..... etc....

or
#!/bin/sh
# This script is just 'sudo shutdown -h now' for Casteret
etc...

I run ~/bin$ ./mkbinindex > ls.html

which make a nice page for them all.




Mike --
Mike Lake
Caver, Linux enthusiast and interested in anything technical.
#!/usr/bin/perl -w
# Makes list of all bin directories, files and their comments.
# By "comment" I mean the first line after #!/bin/..... 

print "<HTML>\n";
print "<!-- Index created from mkbinindex -->\n";
print "<HEAD>\n";
print "<TITLE>Mikes ~/bin Files and Directories</TITLE>\n";
print "</HEAD>\n";

print "<BODY FONT=\"fixed\">\n";
print "<CENTER><H1 color=\"red\">Mike's ~/bin Files</H1></CENTER>\n";

print "<CODE>\n";
print "Created with <B>./mkbinindex > ls.html</B><BR>\n";
print "<font color=\"green\">green</font> indicates executable, otherwise not executable.\n";

# We want to place directories at te top of the page.
print "<H3>Directories</H3>";
print "<TABLE>\n";
# First pass through list all directories.
while ( defined($file = <*>) )
{
	if ( -d $file )
	{
		print "<TR><TD><A HREF=\"$file\">$file</A></TR>";
	}
}
print "</TABLE>\n";

# Now we place files next.
print "<H3>Files</H3>";
print "<TABLE>\n";
# Second pass through list all directories.
while ( defined($file = <*>) )
{
	if ( -T $file ) # If file is a text file
	{
		print "<TR><TD><A HREF=\"$file\">";
		if ( -x $file ) # If file is executable.
		{
			print "<font color=\"green\">$file</font></A>";
		}
		else
		{
			print "$file</A>";
		}
		open (IN, "$file") ||
			die "Cannot open file $file for reading\!";
		# If we read in the index file the < in HTML stuffs us up.
		if ( $file ne "index.html" )
		{	
			$line = <IN>; 		# Read in first line and discard.
			$line = <IN>;		# Read in second line.
		}
		else 
		{ 
			$line = "This index."
		}
		$line =~ s/^#\s*//; # Get rid of leading # and zero or more spaces.
		print "<TD>$line</TR>\n";
		close (IN);
	}
}

print "</TABLE>\n";
print "</CODE>\n";
print "<p><HR>";
print "</BODY>\n";
print "</HTML>\n";
	
#!/bin/sh
# Makes html list of files and directories.

# Need to modify this script to generate 
# a_file_name
# <A HREF="a_file_name">a_file_name</A> 

echo "<TITLE>Index of Files and Directories</TITLE>"
echo "<A NAME=\"top\"/>"
echo "<H1>Index of Files and Directories</H1>"
echo "Created with <b>$0 > THISFILE</b>"
echo "<p>"
echo "<table><tr>"
echo "<td valign='top'><h2>Files</h2>"
echo "<UL>"
for i in *; do
	# the '-f' tests if $i exists and is a regular file i.e. not a directory
    if [ -f $i ]; then
	# Bens_linux_ppc_page.html:<!-- MRL Bens pages that .... -->
		description=`grep MRL $i | sed "s/<\!-- MRL \(.*\) -->/\1/"`
		echo "<LI><A HREF=\"$i\">`basename $i`</A>&nbsp;&nbsp;&nbsp;$description"
    fi
done
echo "</UL></td>"

echo "<td valign='top'><H2>Directories</H2>"
echo "<UL>"
for i in *; do
	if [ -d $i ]; then
		echo "<LI><A HREF=\"$i/\">`basename $i`</A>"
	fi
done
echo "</UL></td>"
echo "</tr></table>"
echo "<P>"
echo "<A HREF=\"#top\">top</A>"
echo "<HR>"

# Here is the interesting bit. We don't know what filename the user might 
# have output the script to but we do know that it contains the string
# "THISFILE". 

exit 0