You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

冒号分隔文本转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>' >> $htm uses $htm instead 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

  1. Here-Documents (<<EOF): Makes writing multi-line HTML blocks easier and avoids quote-escaping headaches.
  2. Whitespace Trimming: xargs removes leading/trailing spaces from each field, cleaning up the input format.
  3. Valid HTML: Headers are now wrapped in a <tr> row, which is required for proper table structure.
  4. Safe Filename Handling: "$@" ensures filenames with spaces are processed correctly.
  5. Optional Styling: Added border-collapse and padding to make the table more readable (you can remove these if you prefer plain styling).
  6. Fixed Typos & Redirection: Corrected the $htm typo 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:19:21