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 :-)
Related
I have a text file which has 3 values separated by :
25-08-2019_19.00.00 : Port port1 of URL http://ip1:port1/ is NOT OPEN : Zoom1
25-08-2019_19.00.00 : Port port2 of URL http://ip2:port2/ is NOT OPEN : MP
and so on.
I want to print the output to a html type tabular format file, which has 3 headings, date, output and system and corresponding data in 3 columns.
I tried below code, but it is not putting data to table format.
#! /bin/bash
if [ $# -eq 0 ] ; then
echo "USAGE: $(basename $0) file1 file2 file3 ..."
exit 1
fi
for file in $* ; do
html=$(echo $file | sed 's/\.txt$/\.html/i')
echo "<html>" >> $html
echo "<style type="text/css">
table, th, td {
border: 1px solid black;
}
</style>" >> $html
echo " <body>" >> $html
echo '<table>' >> $htm
echo '<th>HEADING1</th>' >> $html
echo '<th>HEADING2</th>' >> $html
echo '<th>HEADING3</th>' >> $html
while IFS=':' read -ra line ; do
echo "<tr>" >> $html
for i in "${line[#]}"; do
echo "<td>$i</td>" >> $html
# echo "<td>$i</td>" >> $html
done
echo "</tr>" >> $html
done < $file
echo '</table>'
echo " </body>" >> $html
echo "</html>" >> $html
done
If you can, please consider awk:
awk -F' : ' '
BEGIN{
print "<html><style type=\"text/css\">table, th, td {border: 1px solid black;}</style><body><table><th>HEADING1</th><th>HEADING2</th><th>HEADING3</th>"
}
{
print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i "</td>"
print "</tr>"
}
END{
print "</table></body></html>"
}
' input_file > output.html
The BEGIN and END statement fills the static html tags.
The middle statement fills one line of the array according to each input file line by adding <tr> for each line and <td> for each field.
I'm trying to create a shell script that creates an output file with html content & xml content but my goal is open this file as an excel workbook/file (xls format).
This is a part of the code:
Log INF "Creating the first excel file: ${FICHERO_OUT}"
echo "<html>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<style>
.table_main {
border: 1px solid black;
border-top-style: ridge;
border-bottom-style: ridge;
border-left-style: ridge;
border-right-style: ridge;
border-color: black;
}
</style>" >> ${DIR_OUT}/${FICHERO_OUT}
echo " <body>" >> ${DIR_OUT}/${FICHERO_OUT}
echo '<table>' >> ${DIR_OUT}/${FICHERO_OUT}
echo '<th class="table_main" bgcolor="##FFBF00">ERROR</th>' >> ${DIR_OUT}/${FICHERO_OUT}
echo '<th class="table_main" bgcolor="##FFBF00" >DESCRIPTION</th>' >> ${DIR_OUT}/${FICHERO_OUT}
echo "<tr>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<td>0</td>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<td>DES</td>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "</tr>" >> ${DIR_OUT}/${FICHERO_OUT}
echo '</table>' >> ${DIR_OUT}/${FICHERO_OUT}
echo " </body>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "</html>" >> ${DIR_OUT}/${FICHERO_OUT}`
This part of the code works fine, but when I try to modify the Worksheet name, I can't. I try to do it with the next code:
echo "<?xml version="1.0"?>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"" >> ${DIR_OUT}/${FICHERO_OUT}
echo "xmlns:o="urn:schemas-microsoft-com:office:office"" >> ${DIR_OUT}/${FICHERO_OUT}
echo "xmlns:x="urn:schemas-microsoft-com:office:excel"" >> ${DIR_OUT}/${FICHERO_OUT}
echo "xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"" >> ${DIR_OUT}/${FICHERO_OUT}
echo "xmlns:html="http://www.w3.org/TR/REC-html40">" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<Worksheet ss:Name="Student Data">" >> ${DIR_OUT}/${FICHERO_OUT}
echo "<table>" >> ${DIR_OUT}/${FICHERO_OUT}
....
I wrote here the HTML code I mentioned before.
....
echo "</table>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "</Worksheet>" >> ${DIR_OUT}/${FICHERO_OUT}
echo "</Workbook>" >> ${DIR_OUT}/${FICHERO_OUT}
Please, could someone help here? Any suggestion to modify the worksheet name? Any alternative?
When you want to open the file with Excel, generate a .csv file.
When you start the .csv file with a line SEP=x, the character x will be used between fields.
Finally, I've solved it. I did it, using only xml code and adding "sqlplus querys + spool".
Thank you very much for your support.
Best Regards,
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 store my matrix's data in a html file and send a email in outlook ,my code looks as follows:
printf "<!DOCTYPE html>"
printf "<html>"
printf "<head>"
printf "<style>"
printf "</style>"
printf "</head>"
printf "<body>"
printf "<table>"
printf "<tr>"
printf "<th>Total</th>"
printf "<th>StillFail</th>"
printf "<th>Pass</th>"
printf "<th>ScriptError</th>"
printf "<th>APIName</th>"
printf "</tr>"
printf "<tr>"
echo
for ((j=1;j<=num_rows;j++)) do
printf "<tr>"
for ((i=1;i<=num_columns;i++)) do
printf "<td>"
printf "${matrix[$j,$i]}"
printf "</td>"
printf "</tr>"
done
echo
done
printf "</tr>"
printf "</table>"
printf "</body>"
printf "</html>"
#mailx -a 'Content-Type: html' -s "my subject" test#example.com < output.html
mailx -s "TESTING MAIL"</home/test/example/output.html "test#example.com"
I want my output as a well aligned table. Can someone help me on this? TIA
You need to append content-type in header it can be done with -a flag in mailx
comment your last line and try this
mailx -a 'Content-Type: text/html' -s "Test MAIL" test#example.com</home/test/example/output.html
Edit :
As per the OP End error : content-type:texthtml no such file or
directory
To install mailutils in mac
Press Command+Space and type Terminal and press enter/return key.
Run in Terminal app:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
and press enter/return key. Wait for the command to finish.
Run:
brew install mailutils
reference : http://macappstore.org/mailutils/
Maybe you can use this for a base:
(
echo "To: ADRESSES"
echo "Subject: SUBJECT"
echo "Content-Type: text/html"
echo
echo "HTML code"
) | /usr/sbin/sendmail -F "NameOfSender" -t
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 ...