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> [ Index ] </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> [ Index ]</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 e.g.:
gnuplot <<EOF_GNUPLOT
# your gnuplot code here - e.g.:
reset
set term png truecolor enhanced size 1050,1050
set xtics out
# ...
EOF_GNUPLOT
Related
while read row do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
When I execute this fraction of code it gives me this error:
./table_html.csv: line 36: syntax error near unexpected token `echo'
./table_html.csv: line 36: ` echo "<td>$valore</td>" >> $file'
How do I fix it?
I'm not very good with shell scripts, that's why I can not think of anything else.
(Ah I was working on this when the alarm for the oven went off) what I wanted to say was it's just the "do" in the wrong place after the while. So here's your code restated (with a file="filename" added to get it working):
file="test2.html"
while read row
do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
So it was just a line break before the do is all you needed :-)
I have a variable that am declaring like below in my shell script:
Variable = awk 'Some code' filename.txt << this is assigning one word from the text file to this variable
And then am trying to display this in an HTML heading like below, but its not working.
<h2><font color="navy"> Network-element : ${variable} </font></h2>
any help is appreciated.
I think there could be 2 points here.
1- Cover the variable value like:
Variable=$(awk 'Some code' filename.txt)
2- Then in shell script html code doesn't work like simple print you have to use echo for it (NOTE this is only an example)eg->
echo "<html>" > $OUTPUT_FILE
echo "<title>" >> $OUTPUT_FILE
echo "A Test script." >> $OUTPUT_FILE
echo "</title>" >> $OUTPUT_FILE
echo "</body>" >> $OUTPUT_FILE
echo "</html>" >> $OUTPUT_FILE
This above code is only an example where I am putting echo statements into output file. You could use it as per your use case. Also use "$Variable" in echo command.
I want to write a script which will colour lines of the input green if they represent the current date (i.e. on 2017-07-13, the second line should be green), red otherwise.
Input format (CSV):
Feed1,2017-07-01
Feed2,2017-07-13
Feed3,2017-07-03
Here is what I have so far:
while IFS="," read dtts feed; do
if [ "$dtts" == "$DATEVALID" ]; then
echo "<tr>"
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
echo "</tr>"
else
echo "<tr>"
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
echo "</tr>"
fi
done < $INPUTFILE.csv
But it is showing incorrect output (all lines have the same colour).
I see a couple of possible problems with your code. Some have been fixed when editing your original question, please consider formatting your code properly when asking a question on SO.
You are reading fields from the CSV in the incorrect order.
read dtts feed # This means the first field is $dtts, the second is $feed
So the while loop should probably look like
while IFS="," read feed dtts; do
And finally, you did not provide the code responsible for $DATEVALID or $INPUTFILE. An important question is whether $DATEVALID is in the same format as the one used in your input file. To obtain such date within a shell script, you could try:
DATEVALID=`date "+%Y-%m-%d"`
This should give you today's date in the YYYY-MM-DD format you need. To summarise:
DATEVALID=`date "+%Y-%m-%d"`
INPUTFILE="input.csv"
while IFS="," read feed dtts; do
echo "<tr>"
if [ "$dtts" == "$DATEVALID" ]; then
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
else
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
fi
echo "</tr>"
done < $INPUTFILE
First problem -- your data does not match the variables you are specifying in your read command.
First question -- what is the value of $DATEVALID ?
echoing your variables will go a long way toward troubleshooting these types of problems.
Using Kornshell (ksh), I have 3 arrays containing date, filename, content of file - Array size is different everyday.
I need a loop to create a table row for every single element in the arrays and populate them with the data in the arrays using a counter.
Therefore, is there a better way instead of manually repeating and inputting several lines like below (snippet) for every array element?
P.S. - Tried using echo ${array[#] which is only good for the first 2 arrays. But since the 3rd array has the content of every file (very long) it is not coming good as a table format.
...
echo "<tr>"
echo "<td>`echo ${array[2]}`</td>"
echo "<td>`echo ${array[2]}`</td>"
echo "<td><pre>`(cat "${array[2]}")`</pre></td>"
echo "</tr>"
echo "<tr>"
echo "<td>`echo ${array[3]}`</td>"
echo "<td>`echo ${array[3]}`</td>"
echo "<td><pre>`(cat "${array[3]}")`</pre></td>"
echo "</tr>"
...
...
) | /usr/sbin/sendmail -t
You need to iterate
len=${#date[#]}
for ((i=0; i<len; i++)); do
echo "<tr>"
echo "<td>${date[i]}`</td>"
echo "<td>${filename[i]}`</td>"
echo "<td><pre>$(< "${filename[i]}")</pre></td>"
echo "</tr>"
done
You don't need to echo a variable to get its contents, just dereference it.
$(< filename) is equivalent to $(cat filename), but it's built-in to the shell
Like in any language, use a loop:
dates=(...)
filenames=(...)
contents=(...)
for ((i=0; i<${#dates[#]}; i++)); do
echo "<tr>"
echo "<td>${dates[i]}</td>"
echo "<td>${filenames[i]}</td>"
echo "<td><pre>${contents[i]}</pre></td>"
echo "</tr>"
done | sendmail ...
Just a quick question guys. I would like to assign html tags to a variable in BASH. How do i do this?
Or is there a better way to do this?
This is the actual portion of the code:
function storage_util_1942
{
if [ $SETFLAG_1942 = "YES" ];
then
for i in {1..5}
do
TMPSTOR=$(cat $HCREPORT1942 | grep -A$i DISK | tail -1)
RULESET=$(cat $HCREPORT1942 | grep -A$i DISK | tail -1 | awk '{print $6}' | cut -d'%' -f1)
if [ $RULESET -gt 90 ]
then
STORAGE$i="<font color=\"red\"><b>${TMPSTOR}</b></font>"
else
STORAGE$i="<font color=\"green\"><b>${TMPSTOR}</b></font>"
fi
done
fi
echo " <tr align="left">"
echo " <td><font face="verdana,helvetica" size="1" color="red"><b>Storage Capacity <font color="green">[Must be less than 90%]</b></font></td>"
echo " </tr>"
echo " <tr align="left" bgcolor="white" height="80">"
echo " <td><font face="courier new" size="1" color="black">"
echo " <br>"
echo " <pre>"
echo " $STORAGE1"
echo " $STORAGE2"
echo " $STORAGE3"
echo " $STORAGE4"
echo " $STORAGE5"
echo " </pre></font>"
echo " </td>"
echo " </tr>"
}
This is part of an HTML base reporting that I am creating. The objective is to display the value of $STORAGE(#) base on the condition set above. RED if it is above 90 and GREEN otherwise.
When I tried to run the script it still give me the "No such file or directory" error:
+++ storage_util_1942
+++ '[' YES = YES ']'
+++ for i in '{1..5}'
++++ cat /apps/data/support_bin/monitoring/healthcheck/healthcheck.report
++++ grep -A1 DISK
++++ tail -1
+++ TMPSTOR='/dev/mapper/rootdg-rootdgvol2 ext3 1.9G 1.7G 133M 93% /var'
++++ cat /apps/data/support_bin/monitoring/healthcheck/czchols1942.healthcheck.report
++++ grep -A1 DISK
++++ tail -1
++++ awk '{print $6}'
++++ cut -d% -f1
+++ RULESET=93
+++ '[' 93 -gt 90 ']'
+++ 'STORAGE1=<font color="red"><b>/dev/mapper/rootdg-rootdgvol2 ext3 1.9G 1.7G 133M 93% /var</b></font>'
./script.sh: line 166: STORAGE1=<font color="red"><b>/dev/mapper/rootdg-rootdgvol2 ext3 1.9G 1.7G 133M 93% /var</b></font>: No such file or directory
The evaluation before the last line is already correct but how come it evaluates the value as if it is executed? I should only stay as a value and not a command.
Thanks,
You need quotes around the variable assignments, and since your values have double-quotes (and no other variables referenced), I would use single quotes:
var='<font color="red">HELLO WORLD</font>'
If you need to interpret other variables within the assignment, you need to use double-quotes, escaping any literal ones:
var="<font color=\"red\">${TMPSTOR}</font>"
You also need to use arrays for your "dynamically-named" variables:
STORAGE[$i]="..."
echo "$STORAGE[1]" ...
The bigger question is what you're really trying to do with this. Why are you using a <font> tag, why aren't you just setting a CSS class as the variable (since that's the only difference in the HTML), etc.
There are lots of things in the script segment you've posted that could be done in "a better way". Here's a sampling of them.
Don't use unnecessary pipes. awk will do everything that grep, tail and cut will do, without launching extra processes for each step through the pipeline.
TMPSTOR=$(awk -vi="$i" '/DISK/{n=i+1} {n--} n==0 {print;exit}' $HCREPORT1942)
Use quotes around variables. You've included if [ $SETFLAG_1942 = "YES" ]; which is backwards. The thing you want to protect in quotes is the variable, not the text that is hard-coded into the script.
You're using dynamic variable naming. This is a Bad Thing, as it usually requires use of eval, which is evil.
eval STORAGE$i="'<font color=\"red\"><b>\${TMPSTOR}</b></font>'"
So instead of STORAGE$i=..., use an array.
declare -a STORAGE
if [ -n "${RULESET//[0-9]/}" ]; then
# RULESET contains non-digts, which it should not.
echo "ERROR: invalid data, please investigate." >&2
exit 1
elif [ "$RULESET" -gt 90 ]; then
STORAGE[$i]="<span class='warn'>${TMPSTOR}</span>"
else
STORAGE[$i]="<span class='good'>${TMPSTOR}</span>"
fi
Then walk through the array with something like:
for item in ${STORAGE[#]}; do
echo "$item"
done
And finally, your HTML needs to be improved, but that's off-topic for this question.