冒号分隔文本转HTML表格:Bash脚本无法生成表格的问题排查
Fixing Your Bash Script to Generate HTML Tables
Let's walk through the issues in your original script and fix them to produce the correct HTML table with date, output, and system columns:
Key Problems in the Original Script
- Typo:
echo '<table>' >> $htmuses$htminstead of$html, so the table opening tag never gets written to the output file. - Missing Redirection: The closing
</table>line is printed to the terminal instead of being appended to the HTML file. - Invalid HTML Structure:
<th>tags were placed directly under<table>(they need to be inside a<tr>row). - Generic Headings: The headers were labeled HEADING1-3 instead of the required
date,output,system. - Leading Spaces in Fields: Your input lines have spaces after colons (e.g.,
date : output : system), which left unwanted leading spaces in each table cell. - Quote Syntax Error: The style tag used double quotes inside an echo with double quotes, breaking the string prematurely.
- Unsafe Filename Handling: Using
$*instead of"$@"fails for filenames containing spaces.
Corrected Script
#!/bin/bash if [ $# -eq 0 ] ; then echo "USAGE: $(basename $0) file1 file2 file3 ..." exit 1 fi for file in "$@" ; do # Generate output HTML filename (replace .txt with .html, case-insensitive) html=$(echo "$file" | sed 's/\.txt$/\.html/i') # Write initial HTML structure using a here-document for cleaner multi-line content cat > "$html" <<EOF <html> <style type='text/css'> table, th, td { border: 1px solid black; border-collapse: collapse; /* Optional: Merges borders for a cleaner look */ padding: 8px; /* Optional: Adds space inside cells */ } th { background-color: #f2f2f2; /* Optional: Gray background for headers */ } </style> <body> <table> <tr> <th>date</th> <th>output</th> <th>system</th> </tr> EOF # Process each line in the input file while IFS=':' read -r raw_date raw_output raw_system; do # Trim leading/trailing whitespace from each field date=$(echo "$raw_date" | xargs) output=$(echo "$raw_output" | xargs) system=$(echo "$raw_system" | xargs) # Append formatted table row to the HTML file echo " <tr>" >> "$html" echo " <td>$date</td>" >> "$html" echo " <td>$output</td>" >> "$html" echo " <td>$system</td>" >> "$html" echo " </tr>" >> "$html" done < "$file" # Close the HTML structure cat >> "$html" <<EOF </table> </body> </html> EOF echo "Successfully generated: $html" done
Breakdown of Improvements
- Here-Documents (
<<EOF): Makes writing multi-line HTML blocks easier and avoids quote-escaping headaches. - Whitespace Trimming:
xargsremoves leading/trailing spaces from each field, cleaning up the input format. - Valid HTML: Headers are now wrapped in a
<tr>row, which is required for proper table structure. - Safe Filename Handling:
"$@"ensures filenames with spaces are processed correctly. - Optional Styling: Added border-collapse and padding to make the table more readable (you can remove these if you prefer plain styling).
- Fixed Typos & Redirection: Corrected the
$htmtypo and ensured all content is written to the HTML file instead of stdout.
Testing this script with your sample input will produce a valid, clean HTML table with the three desired columns.
内容的提问来源于stack exchange,提问作者Novice
相关产品推荐
相关产品推荐

