简体   繁体   中英

CGI : How to put Gnuplot command in my bash script?

I am setting up a bash/html CGI that will allow me to generate graphs with GnuPlot.

For this, I have two CGI pages in bash/html:

The first page contains a simple "generate" button that opens a new page and executes my gnuplot script, and a listbox that will have been previously filled with the names of different clusters.

The second page that is opened via the "generate" button displays my graph.

My question is: how do I put gnuplot commands in my CGI page in bash/html ?

Here is the code of my first page that allows me to choose the cluster that interests me :

#!/bin/bash


echo "Content-type: text/html"
echo ""

echo '
<html>
        <head>
                <meta http-equiv="Content-Type" content="test/html"; charset=UTF-8">
                <title> CLUSTER GRAPH </title>
                <h1> Cluster Graph <font size=3> <a href="Index.sh">[ Index ]</a> </font> </h1>
                <hr size="4" color="blue">
        </head>
<body>

<p> Choose a Cluster and press the button to generate the graph ! </p>'

Cluster_Name=$(cat ClusterFullList.csv | awk '{print $3}' | sort |uniq)

echo "<form action="Script_test.sh" method="post">"
echo "<select name="CLUSTER">"
echo "$Cluster_Name" | while read CLUSTER; do
        echo " <option value="$CLUSTER">$CLUSTER</option>"
        done
echo "</select>"
        echo"<br><br>"
        echo "<input type="submit" value="Generate">"
echo "</form>"





echo '
</body>
</html> 
'

Here is the code of my second page on which my graph should appear:

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo "
<html>
        <head>
                <title> CLUSTER GRAPH </title>
                <h1> Cluster Graph <font size=3> <a href="Index.sh">[ Index ]</a></font></h1>
                <hr size="4" color="blue">
        </head>
<body>
<img src="$test.png">
<PRE>"

read a

test=`echo $a | cut -d'=' -f2`
echo "$test" 

echo " f(w) = (strlen(w) > 10 ? word(w, 1) . "\n" . word(w, 2) : w) " >> gnuplot_script_test.txt
echo " set title $test " >> gnuplot_script_test.txt
echo " set terminal png truecolor size 960, 720 " >> gnuplot_script_test.txt
echo ' set output "$test.png" ' >> gnuplot_script_test.txt
echo " set bmargin at screen 0.1 " >> gnuplot_script_test.txt
echo " set key top center " >> gnuplot_script_test.txt
echo " set grid " >> gnuplot_script_test.txt
echo " set style data histograms " >> gnuplot_script_test.txt
echo " set style fill solid 1.00 border -1 " >> gnuplot_script_test.txt
echo " set boxwidth 0.7 relative " >> gnuplot_script_test.txt
echo " set yrange [0:] " >> gnuplot_script_test.txt
echo " set format y "%g%%" " >> gnuplot_script_test.txt
echo " set datafile separator "," " >> gnuplot_script_test.txt
echo ' plot "/var/www/cgi-bin/CLUSTER_1.txt" using 2:xtic(f(stringcolumn(1))) title " CPU consumption (%) ", '' using 3 title " RAM consumption (%)", '' using 0:($2+1):(sprintf("%g%%",$2)) with labels notitle, '' using 0:($3+1):(sprintf("     %g%%",$3)) with labels notitle ' >> gnuplot_script_test.txt

gnuplot gnuplot_script_test.txt
rm gnuoplot_script_test.txt
echo "
</PRE>
</body>
</html>
"

The idea is :

The commands:

read a

test=`echo $a | cut -d'=' -f2`
echo "$test" 

Allow me to retrieve my query string which is the name of the cluster I am interested in (cluster name which is in the list box of my first page)

I execute the gnuplots commands and send them each time in the file "gnuplot_script_test.txt", then I execute it via the command :

gnuplot gnuplot_script_test.txt

Then the txt file deletes itself via the command :

rm gnuplot_script_test.txt

I would like to make sure that the name of the cluster I retrieve via my query string, I can put it in my gnuplot commands. To do this, I put my variable "$test" (which contains the name of my cluster) in the lines :

echo" set title "$test"" 

To put the name of my cluster in my title

echo ' set output "$test.png" ' 

To give my png the name of my cluster

But, when I choose my cluster on my first page, that I click on generate, the name of my cluster appears well on my second page, but the image is not generated...

Can you tell me why ?

Thank you !

You are using $test variable before you set it up.

You can find helpful heredoc http://tldp.org/LDP/abs/html/here-docs.html Then you can do eg:

gnuplot <<EOF_GNUPLOT
  # your gnuplot code here - e.g.:
  reset
  set term png truecolor enhanced size 1050,1050
  set xtics out
  # ...
EOF_GNUPLOT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM