Search code examples
htmlarraysbashcgi

How Can I fill an html array with my awk script?


For my CGI in bash/html, I try to display some informations from many CSV files in html array. So I use this script :

#!/bin/bash

echo "Content-type: text/html"
echo ""
echo '
<html>
        <head>
                <meta http-equiv="Content-Type" content="test/html"; charset=UTF-8">
                <title> CLF MONITORING </title>
                <h1> FRAME monitoring <font size=3> <a href="Index.sh">[ Index ]</a> </font> </h1>
                <hr size="4" color="blue">

        <style>
                         body{
                          background-color: #eff1f0;
                         }
        </style>

        </head>
<body>'

read a
test=$( echo $a | cut -d'=' -f2)

echo '<PRE>'

echo "FRAME : $test "
echo "------------------"

echo ""


echo "<table>"
echo "<tr>"
echo "<td>"
echo "<PRE>"
for fn in /var/www/cgi-bin/LPAR_MAP/*; do 
    awk -F',|;' 'NR==1 { split(FILENAME ,a,"[-.]");
       print "DATE ========================== : " a[4] }
       /'$test'/ { 
           print ""
           print "LPARS :" $2
           print "RAM : " $5
           print "CPU 1 : " $6
           print "CPU 2 : " $7
           print "" 
           print ""}' $fn; 
done
echo "</PRE>"
echo "</td>"
echo "</tr>"
echo "</table>"
echo '</PRE>'
echo '</body>
</html>'

And the output is :

FRAME : MIAIBYC00 
------------------

DATE ========================== : 20180705

LPARS :HSCL9010 This operation is only allowed when the managed system is in the Standby or Operating state.
RAM : 
CPU 1 : 
CPU 2 : 


DATE ========================== : 20180122

LPARS :miaibv228
RAM : 8
CPU 1 : 2.0
CPU 2 : 4



LPARS :miaibv158
RAM : 5
CPU 1 : 0.1
CPU 2 : 1



LPARS :miaibv157
RAM : 6
CPU 1 : 0.2
CPU 2 : 1



LPARS :OSvalidation
RAM : 0.25
CPU 1 : 0.2
CPU 2 : 1



LPARS :miaibv104
RAM : 64
CPU 1 : 3.0
CPU 2 : 7

... 

DATE ========================== : 20180124

LPARS :miaibv228
RAM : 8
CPU 1 : 2.0
CPU 2 : 4



LPARS :miaibv158
RAM : 5
CPU 1 : 0.1
CPU 2 : 1



LPARS :miaibv157
RAM : 6
CPU 1 : 0.2
CPU 2 : 1



LPARS :OSvalidation
RAM : 0.25
CPU 1 : 0.2
CPU 2 : 1

But I would like to create many columns, something like this : enter image description here

The idea is : create one column for one " Date ========== " with his informations below, However the number of column.

For exemple :

Date ========= XXXXX    Date ============ XXXXX    Date ============== XXXXXXX

LPARS :miaibv228        LPARS : XXXXXX             LPARS : XXXXX
RAM : 8                 RAM : XXXXXX               RAM : XXXXXX
CPU 1 : 2.0             CPU 1 : XXXXX              CPU 1 : XXXXX
CPU 2 : 4               CPU 2 :  XXXXX             CPU 2 : XXXXXX

For this exemple, I have only 3 " dates " and some informations. But in reality, I have a lot more informations.

Do you have any idea to do this ?


Solution

  • I think you just need to move your <td> and <pre> tags inside the loop:

    . . .
    for fn in /var/www/cgi-bin/LPAR_MAP/*; do 
        echo "<td>"
        echo "<PRE>"
        awk -F',|;' 'NR==1 { split(FILENAME ,a,"[-.]");
           print "DATE ========================== : " a[4] }
           /'$test'/ { 
               print ""
               print "LPARS :" $2
               print "RAM : " $5
               print "CPU 1 : " $6
               print "CPU 2 : " $7
               print "" 
               print ""}' $fn; 
        echo "</PRE>"
        echo "</td>"
    done
    . . .