【问题标题】:How to collect filename, last modified date and last modified date +30 days and add to HTML table?如何收集文件名、最后修改日期和最后修改日期 +30 天并添加到 HTML 表格?
【发布时间】:2022-11-23 04:12:08
【问题描述】:

我正在尝试编写一个 bash 脚本,它将获取目录的内容并将文件名(减去扩展名)、最后修改日期和最后修改日期 + 30 天打印到 HTML 表中。例如:

Filename Last Modified Date Last Modified Date + 30 days
Test Fri 18 Nov 12:35:00 PM EST Sun 18 Dec 12:35:00 PM EST

到目前为止,我已经设法编写了一个脚本来创建 HTML 表并添加不带扩展名的文件名,但是我正在努力为其他两列添加数据。到目前为止,这是我的脚本:

#!/bin/bash



head='<!DOCTYPE html>
<html>
        <div class="u-expanded-width u-table u-table-responsive u-table-1">
          <table class="u-table-entity u-table-entity-1">
            <colgroup>
              <col width="33.3%">
              <col width="33.3%">
              <col width="33.3%">
            </colgroup>
            <thead class="u-align-center u-custom-font u-grey-5 u-heading-font u-table-header u-table-header-1">
              <tr style="height: 40px;">
                <th class="u-border-1 u-border-grey-dark-1 u-table-cell">Community String</th>
                <th class="u-border-1 u-border-grey-dark-1 u-table-cell">Simulation Created</th>
                <th class="u-border-1 u-border-grey-dark-1 u-table-cell">Simulation Expires</th>
              </tr>
            </thead>
            <tbody class="u-align-center u-table-body">
            <tr style="height: 7px;">
            </tr>'

tail='</tbody>
</table>
</html>'

printf '%s\n' "$head"

shopt -s nullglob

uploaddate="$(find /usr/local/data/ . -maxdepth 1 -type f -name "*.snmprec" -printf "<tr><td>%Tc</td></tr>\n")"

expirydate="$(find /usr/local/data/ -mtime +30 -type f -name "*.snmprec")"

for file in /usr/local/snmpsim/data/ephemeral/*.snmprec; do
  [[ $file =~ ([^/]+).snmprec$ ]] &&
  Filename=${BASH_REMATCH[1]}
  SimulationCreated=$uploaddate
  SimulationExpires=$expirydate
  printf ' <tr>\n    <td>%s</td>\n    <td>%s</td>\n    <td>%s</td>\n </tr>\n' "$Filename" "$SimulationCreated" "$SimulationExpires"
done

我下面的命令收集最后修改日期,但是它将所有日期打印到文件名列中表的每一行中,如下所示:

uploaddate="$(find /usr/local/data/ . -maxdepth 1 -type f -name "*.snmprec" -printf "<tr><td>%Tc</td></tr>\n")"
Filename Last Modified Date Last Modified Date + 30 days
Test
Fri 18 Nov 12:35:00 PM EST

我怎样才能获得我需要的详细信息并像上面的第一个一样在表格中格式化它们?

【问题讨论】:

    标签: bash


    【解决方案1】:

    这是一个使用 GNU 工具的解决方案。它使用 GNU find 获取修改日期 + 文件名,然后使用 GNU awk 获取表格行,负责 HTML 转义。

    #!/bin/bash
    
    cat <<'EOF'
    <!DOCTYPE html>
    <html>
    ... (the rest of your "$head")
    EOF
    
    find /usr/local/data/ . -maxdepth 1 -type f -name '*.snmprec' -printf '%T@ %f
    猜你喜欢
    • 2010-09-17
    • 2017-12-23
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多