CGI:如何将Gnuplot命令放入我的bash脚本中?

时间:2019-04-18 09:21:52

标签: html bash gnuplot cgi

我正在设置一个bash / html CGI,它将允许我使用GnuPlot生成图形。

为此,我在bash / html中有两个CGI页面:

第一页包含一个简单的“生成”按钮,它会打开一个新页面并执行我的gnuplot脚本,还有一个列表框,该列表框之前已填充了不同集群的名称。

通过“生成”按钮打开的第二页显示我的图表。

我的问题是:如何将gnuplot命令放在bash / html的CGI页面中?

这是我第一页的代码,可让我选择自己感兴趣的集群:

#!/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> 
'

这是我的图形应出现在第二页的代码:

#!/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>
"

这个想法是:

命令:

read a

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

允许我检索查询字符串,这是我感兴趣的集群的名称(集群名称位于第一页的列表框中)

我执行gnuplots命令并将其每次发送到文件“ gnuplot_script_test.txt”中,然后通过命令执行该命令:

gnuplot gnuplot_script_test.txt

然后txt文件通过以下命令将自身删除:

rm gnuplot_script_test.txt

我想确保通过查询字符串检索到的集群名称,可以将其放在gnuplot命令中。为此,我将变量“ $ test”(包含集群的名称)放在行中:

echo" set title "$test"" 

在标题中添加群集的名称

echo ' set output "$test.png" ' 

为我的png命名我的集群

但是,当我在第一页上选择集群时,单击了generate,集群的名称在第二页上显示的很好,但是未生成图像...

你能告诉我为什么吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

在设置变量之前,您正在使用$test变量。

您可以在此处找到有用的文档doc http://tldp.org/LDP/abs/html/here-docs.html 然后您可以执行以下操作:

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

答案 1 :(得分:0)

感谢您的回答@lojza!

我尝试:

#!/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>
<PRE>"

read a 

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

echo $test 

gnuplot << EOF_GNUPLOT

reset
f(w) = (strlen(w) > 10 ? word(w, 1) . "\n" . word(w, 2) : w) 
set title "$test"
set terminal png truecolor size 960, 720
set output "$test.png"
set bmargin at screen 0.1 
set key top center 
set grid 
set style data histograms
set style fill solid 1.00 border -1
set boxwidth 0.7 relative
set yrange [0:] 
set format y "%g%%" 
set datafile separator "," 
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

EOF_GNUPLOT 

rm gnuplot_script_test.txt

echo "
</PRE>
<img src="$test.png">
</body>
</html>
"

图像是在我的文件/ cgi-bin /中生成的(我使用RedHat 7.6),但不是作为图像而是作为一个简单文件(例如txt文件,我可以使用gedit打开它作为示例),但是使用名称“ my_cluster.png”

这是我的错误日志:

[Thu Apr 18 15:40:07.035719 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: , referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035752 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: gnuplot> </html>, referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035776 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215:          ^, referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035837 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215:          line 0: invalid command, referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035846 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: , referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035853 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: , referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035877 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: gnuplot> "", referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035922 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215:          ^, referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035982 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215:          line 0: invalid command, referer: http://localhost/cgi-bin/Index.sh
[Thu Apr 18 15:40:07.035992 2019] [cgi:error] [pid 28252] [client ::1:40352] AH01215: , referer: http://localhost/cgi-bin/Index.sh