LinuxQuestions.org
Visit Jeremy's Blog.
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 04-26-2013, 04:27 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question Create an html page by using values generated by another script


Hi all I need to create a table and the values inside the table are to be fetched by running a shell script

The steps involved are as follows Example:
> ./script1
OUTPUT:
A 3
B 5
C 1
and so on

> ./script1
Output:
A 6
B 2
X 8

Basically im getting some values for each user (A,B,C etc) These are the results in various subjects i.e script1 gives me marks of students in Maths, script2 gives marks of students in Science etc. Note: script1, 2 are shell scripts returning output on screen.

Now I need this data as a table or categorised form html page. i.e. A page having headings like MATHS SCIENCE SST in the rows I have users (A,B,C) and each column contains marks for the user taken from script1, script2 upto script n for n subjects

So for doing this I run the master script -< which in turn runs script1,2...n and create the corresponding html by printing values categorically or in some table form, which can be read easily.
I hope i am clear what the requirement is. Many thanks
 
Old 04-26-2013, 08:42 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you really want text -> html, try http://txt2html.sourceforge.net/.
Alternately, if you just need a text layout that looks like a tabular report, then re-direct your scripts' outputs to a file and write some code in eg bash, Perl, whatever to re-format it.

Some good bash links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Perl
http://www.tizag.com/perlT/index.php
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
Old 04-26-2013, 11:12 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your sample output does not contain any HTML markup. How does it differ from the input data? How do you want the table to look? Does the output need to create a complete HTML page, or just the markup related to the table itself? What does the input data look like?

It sounds like your data is embedded in the script. This is generally not a good practice; data and code should be separate entities. A script should run, reading the data from a specified data file.

Code:
<H1>MATH</H1>
<TABLE>
   <TR>
      <TD>A</TD>
      <TD>3</TD>
   </TR>
   <TR>
      <TD>B</TD>
      <TD>5</TD>
   </TR>
   <TR>
      <TD>C</TD>
      <TD>1</TD>
   </TR>
</TABLE>
Is the above what you want your output to look like? More detail is needed.

--- rod.
 
Old 04-26-2013, 06:29 PM   #4
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
Thumbs up Attempted solution

I have written a bash script but had to make some assumptions. Also to make it less abstract I work with example data - real subjects, names and marks. This bash script assumes student list is the same for each script, so if ./maths outputs the scores for John, Frank and Betty in that order than so does english, science etc. and that the script is named as per the academic subject, so ./maths, ./science, ./english rather than literally script1, script2, script3.

To be clear I imagine ./maths outputs:
Code:
John 30
Frank 50
Betty 10
./english outputs:
Code:
John 100
Frank 10
Betty 5
and that the expected output is a table:

Code:
         maths   english
John     30      100
Frank    50      10
Betty    10      5
The script is as follows:

master.sh:
Code:
#!/bin/bash
#

SUBJECTS[0]='maths';
SUBJECTS[1]='science';
SUBJECTS[2]='english';
SUBJECTS[3]='gym';

for SUBJECT in "${SUBJECTS[@]}"
do
	while IFS=$'\n' read LINE
	do
		read STUDENT RESULT <<< "$LINE"
		eval "$STUDENT=\$${STUDENT}\<td\>$RESULT\</td\>"
	done <<< "$(./$SUBJECT)"
done

echo "<table>"
echo "<tr><td>&nbsp;</td>"
for SUBJECT in "${SUBJECTS[@]}"
do
	echo "<td>$SUBJECT</td>"
done
echo "</tr>"
while IFS=$'\n' read LINE
do
	read STUDENT RESULT <<< "$LINE"
	eval "echo \"<tr><td>$STUDENT</td>\${$STUDENT}<tr>\""
done <<< "$(./${SUBJECTS[0]})"
echo "</table>"

Last edited by leehanken; 04-26-2013 at 06:37 PM.
 
1 members found this post helpful.
Old 04-26-2013, 07:07 PM   #5
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
Just an amendment, in case the scripts are literally called ./script1 ./script2 etc. Still the headings have to come from somewhere so at the moment subject names are still hard coded.

Code:
#!/bin/bash
#

SUBJECTS[0]='maths';
SUBJECTS[1]='science';
SUBJECTS[2]='english';
SUBJECTS[3]='gym';


N=0
for SUBJECT in "${SUBJECTS[@]}"
do
	let N++
	while IFS=$'\n' read LINE
	do
		read STUDENT RESULT <<< "$LINE"
		eval "$STUDENT=\$${STUDENT}\<td\>$RESULT\</td\>"
	done <<< "$(./script$N)"
done

echo "<table>"
echo "<tr><td>&nbsp;</td>"
for SUBJECT in "${SUBJECTS[@]}"
do
	echo "<td>$SUBJECT</td>"
done
echo "</tr>"
while IFS=$'\n' read LINE
do
	read STUDENT RESULT <<< "$LINE"
	eval "echo \"<tr><td>$STUDENT</td>\${$STUDENT}<tr>\""
done <<< "$(./script1)"
echo "</table>"
 
1 members found this post helpful.
Old 04-27-2013, 09:55 AM   #6
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Question

thanks leehankin for your code.
Well the scenario is by running ./math (lets change script name), I get a list of all students, but the students who didnt give the paper or scored 0 or any (not applicable) candidate is "NOT REPORTED" by the script. However I need only some students who are a part of my class so from 'n' students reported i used egrep on output to have names of students in my class only
i.e.
./math may return
Quote:
John 30
Frank 50
Betty 10
but ./science may return
Quote:
John 22
Alex 50
Betty 6
The names are in sorted form( which makes no difference to us; just an additional information)
Although I would have loved to have a table like (what u made)
Quote:
maths english
John 30 100
Frank 50 10
Betty 10 5
having a blank or 0 in case there were no value returned for that student.

Since this no value box makes the problem more complex. (and can be taken 1nce i get the easier thing done)


I'm ok having a master script which runs ./math.pl ./science.pl ./sst.pl
  • Note script name is originally xzy.pl
  • but since I know its for xyz.pl is for maths I want a table named maths having data given by script
  • then again pqr.pl create table named science and put it in another table with some gap away from maths table and so on
  • I have 8 such subjects with max rows=14, eg. maths may have 10 rows but sst may have only 4 rows

I hope im clear enough.

@chrism01 The data im getting is from perl script whose working is a black box to me ( im new to it), so I'm trying to utilize that output. I'm assuming you are saying that rather than making a new script I must change the math.pl and science.pl to do the work directly ..right?

Last edited by shridhar22; 04-27-2013 at 09:59 AM.
 
Old 04-27-2013, 10:44 AM   #7
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
If each subject can be an individual table, and it doesn't matter about 0 results, then the master script just really needs to add markup, as suggested by theNbomr.

The following will put the results side by side by nesting one HTML table inside another.

(Assumes scripts are './maths.pl' './science.pl')

Code:
#!/bin/bash
#

declare -a SUBJECTS=('maths' 'science' 'english' 'gym');

echo "<TABLE><TR>"

for SUBJECT in "${SUBJECTS[@]}"
do
	echo "<TD VALIGN=TOP>"
	echo "<STRONG>$SUBJECT</STRONG>"
	echo "<TABLE>"
	while IFS=$'\n' read LINE
	do
		read STUDENT RESULT <<< "$LINE"
		echo "<TR><TD>$STUDENT</TD><TD>$RESULT</TD></TR>"
	done <<< "$(./$SUBJECT.pl)"
	echo "</TABLE>"
	echo "</TD>"
done

echo "</TR></TABLE>"

Last edited by leehanken; 04-27-2013 at 11:01 AM. Reason: more concise array declaration and vertical align
 
Old 04-27-2013, 11:32 AM   #8
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks lee, could you please tell where this is being echoed? I mean this should be written in an HTML file right? with a .html extension? and what does
Quote:
IFS=$'\n' read LINE
done <<< "$(./$SUBJECT.pl)"
do?
 
Old 04-27-2013, 03:07 PM   #9
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
Quote:
Originally Posted by shridhar22 View Post
Thanks lee, could you please tell where this is being echoed? I mean this should be written in an HTML file right? with a .html extension?
It goes to standard output. To write to a file, run:
Code:
./master.sh > output.html
Quote:
and what does
Code:
IFS=$'\n' read LINE
done <<< "$(./$SUBJECT.pl)"
do?
This reads a line at a time from the output of the shell script. To break it down:-

Code:
IFS=$'\n' read LINE
sets the seperator to newline character

Code:
IFS=$'\n' read LINE
reads a piece of text and stores in LINE variable

Code:
do ... done
repeats some commands for each line

Code:
<<< $(./$SUBJECT.pl)
gets standard input for the preceeding read command from the following variable (which will contain the lines of output from the script)

Code:
"<<< $(./$SUBJECT.pl)"
runs the script with the subject name prefixing with ./ and adding .pl to the end

I hope this makes sense.
 
1 members found this post helpful.
Old 04-28-2013, 02:54 AM   #10
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
thanks lee, actually the commands were
Quote:
something.pl -option1 this -option2 that -option3 etc
, but rather than modifying your script
Quote:
('maths' 'science' 'english' 'gym')
I made another .sh script which would run the whole command and named that script math,science,etc. So the problem is almost solved. Just a bit of formatting of html page is left and that would be gr8 for me. Currently the page shows output like this

Quote:
CLASS6_STUDENT_MATHS CLASS6_STUDENT_SCIENCE
Lee 100
Alex 10
John 5
And the other tables are just 1 space away

Therefore the readability is not very good, Can i have these in a better formatting something like
1. Having outlines for each table
2. More space between each table
3. A main heading "Test Results at the top of the page"


And 1 more thing I forgot to mention, the page also has to show last year results as well ( done through another script eg. math_old.pl, science_old.pl,etc) which must come once all this year result tables are done. So should I add another simillar code again eg.
Quote:
#!/bin/bash
#

declare -a SUBJECTS=('maths' 'science' 'english' 'gym');

echo "<TABLE><TR>"

for SUBJECT in "${SUBJECTS[@]}"
do
echo "<TD VALIGN=TOP>"
echo "<STRONG>$SUBJECT</STRONG>"
echo "<TABLE>"
while IFS=$'\n' read LINE
do
read STUDENT RESULT <<< "$LINE"
echo "<TR><TD>$STUDENT</TD><TD>$RESULT</TD></TR>"
done <<< "$(./$SUBJECT.pl)"
echo "</TABLE>"
echo "</TD>"
done

echo "</TR></TABLE>"

declare -a SUBJECTS_old=('maths_old' 'science_old' 'english_old' 'gym_old');

echo "<TABLE><TR>"

for SUBJECT in "${SUBJECTS_old[@]}"
do
echo "<TD VALIGN=TOP>"
echo "<STRONG>$SUBJECT</STRONG>"
echo "<TABLE>"
while IFS=$'\n' read LINE
do
read STUDENT RESULT <<< "$LINE"
echo "<TR><TD>$STUDENT</TD><TD>$RESULT</TD></TR>"
done <<< "$(./$SUBJECT_old.pl)"
echo "</TABLE>"
echo "</TD>"
done

echo "</TR></TABLE>"
however this would over write previous tables

I can do more formatting in future, I just need a readable html by monday. So the page looks like:
Quote:
<centre>TEST RESULTS</centre>
<sub heading> 2013 results</sub heading>
Tables with little more spaces b/w them.

LINE OR GAP OR SEPARATOR OR SIMPLY
=========================
sub heading2 2012 results in the same way as above tables
Many thanks for the help

Last edited by shridhar22; 04-28-2013 at 03:01 AM. Reason: trying to show how table is looking, and how i want to make it look
 
Old 04-28-2013, 07:26 AM   #11
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
Hi, the following is based on the example above. (At this stage I can see improvements that could be made (perhaps writing it in perl), commenting, using stylesheets etc.), but to keep things consistent it is basically the same format as before.

(In order to get the headings from the name of the script, assumes scripts are called './maths.pl' './science.pl' for this year, and './maths_old.pl' './science_old.pl' for last year.)

Code:
#!/bin/bash
#

declare -a SUBJECTS=('maths' 'science' 'english' 'gym');

echo "<CENTER><h1>TEST RESULTS<H1></CENTER>";
echo "<H2>2013 results</H2>";

echo "<TABLE><TR>"

for SUBJECT in "${SUBJECTS[@]}"
do
echo "<TD VALIGN=TOP>"
echo "<H3>$SUBJECT</H3>"
echo "<TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0>"
while IFS=$'\n' read LINE
do
read STUDENT RESULT <<< "$LINE"
echo "<TR><TD>$STUDENT</TD><TD>$RESULT</TD></TR>"
done <<< "$(./$SUBJECT.pl)"
echo "</TABLE>"
echo "</TD><TD>&nbsp;</TD>"
done

echo "</TR></TABLE>"

echo "<HR>"

declare -a SUBJECTS_old=('maths' 'science' 'english' 'gym');

echo "<H2>2012 results</H2>";

echo "<TABLE><TR>"

for SUBJECT in "${SUBJECTS_old[@]}"
do
echo "<TD VALIGN=TOP>"
echo "<H3>$SUBJECT</H3>"
echo "<TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0>"
while IFS=$'\n' read LINE
do
read STUDENT RESULT <<< "$LINE"
echo "<TR><TD>$STUDENT</TD><TD>$RESULT</TD></TR>"
done <<< "$(./${SUBJECT}_old.pl)"
echo "</TABLE>"
echo "</TD><TD>&nbsp;</TD>"
done

echo "</TR></TABLE>"
 
Old 04-28-2013, 11:34 AM   #12
leehanken
LQ Newbie
 
Registered: Jan 2009
Posts: 14
Blog Entries: 1

Rep: Reputation: 7
In case it is more useful, here is how I would write it in Perl. Personally, I prefer this, because the headings can be named differently to the scripts, and also it puts each year into one big table rather than several side by side, and also handles if a student does not have a certain result for a subject.

master.pl:
Code:
#!/usr/bin/perl
#

%subjects_new = ("Maths" => "maths.pl",
		"English" => "english.pl",
		"Science" => "science.pl",
		"Gym" => "gym.pl");

%subjects_old = ("Maths" => "maths_old.pl",
		"English" => "english_old.pl",
		"Science" => "science_old.pl",
		"Gym" => "gym_old.pl");

print "<html>";
print "<head><title>Results</title></head>";
print "<body>";
print "<h1>TEST RESULTS</h1>";
print "<h2>2013 Results</h2>";
show_results(\%subjects_new);
print "<hr>";
print "<h2>2012 results</h2>";
show_results(\%subjects_old);
print "</body>";
print "</html>";

sub show_results {
	$ref = shift;
	%subjects = %$ref;
	my %student_marks = ();
	my %students = ();
	
	while ( ($subject, $script) = each(%subjects) ) {
		$output = qx("./$script");
		foreach my $line (split /[\r\n]+/, $output) {
			($student, $result) = split (/\ /, $line );
			$student_marks{$student}{$subject}=$result;
			$students{$student}=1;
		}
	}
		
	print "<table border=1 cellpadding=10 cellspacing=0>";
	print "<tr><td>&nbsp;</td>";
	foreach $subject ( keys %subjects ) {
		print "<th>$subject</th>";
	}
	print "</tr>";
	foreach $student (sort keys %students) {
		print "<tr><th>$student</th>";
		foreach $subject ( keys %subjects ) {
			$result='-';
			$result=$student_marks{$student}{$subject} if exists $student_marks{$student}{$subject};
			print "<td>$result</td>";
		} 
		print "</tr>";
	}
	print "</table>";
}
 
  


Reply

Tags
html, shell scripting



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
script generated html using external CSS andySMI Linux - Software 4 11-14-2012 05:57 AM
Create Simple HTML page from a README with Anchors kushalkoolwal General 1 10-31-2010 10:18 PM
Using lynx to create html page with the file structure of a local directory advent73 Linux - Newbie 1 10-10-2010 12:56 PM
How to create a HTML page to hide the running process of software LinpusKelvin Linux - Newbie 4 07-08-2010 05:49 AM
Html generated by a CGI page counter script displayed in browser without being parsed gregorian Programming 5 08-19-2009 10:41 AM

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

All times are GMT -5. The time now is 09:07 AM.

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