Create an html page by using values generated by another script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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> </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>"
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> </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>"
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.
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
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
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_MATHSCLASS6_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
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> </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> </TD>"
done
echo "</TR></TABLE>"
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.