【问题标题】:CGI : How to put Gnuplot command in my bash script?CGI:如何将 Gnuplot 命令放入我的 bash 脚本中?
【发布时间】:2019-04-18 09:21:51
【问题描述】:

我正在设置一个 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 命名我的集群

但是,当我在我的第一页上选择我的集群时,我点击生成,我的集群的名称出现在我的第二页上,但图像没有生成......

你能告诉我为什么吗?

谢谢!

【问题讨论】:

    标签: html bash gnuplot cgi


    【解决方案1】:

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

    您可以找到有用的 heredoc 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
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      相关资源
      最近更新 更多