数学问题或变量值问题我不确定

数学问题或变量值问题我不确定

我在比较纪元时间时遇到问题。我真的不确定我做错了什么。我的任务是识别和处理特定日期范围之外的文件。

在本例中,它是一个清理脚本。它需要一些参数来进行搜索,然后检查日期。我的日期是这样的:

我们可以删除文件的最早日期 ->date -d "-30days" +"%s"
我们可以压缩文件的最早日期 ->date -d "-14days" +"%s"
获取文件的上次修改时间:stat -c "%y" "/path/to/file"
获取上次修改时间的纪元:date -d "<from the obtained time from stat>" +"%s"

数学是这样的:

if (( FILE_TIMESTAMP >= EARLIEST_DELETE_DATE ))
then
    delete file;
elif (( FILE_TIMESTAMP >= EARLIEST_COMPRESS_DATE ))
then
    compress file;
else
    echo "file skipped as it wasnt in teh date ranges";
fi

(编辑上面的代码:我在这里删除了对 <= EARLIEST_DELETE_DATE 的检查)

第一个 if 语句按预期工作。第二个工作中途 - 它确实识别出时间戳大于压缩日期,但检查小于删除日期似乎返回 false,因为该部分代码永远不会运行。

是我计算错误还是变量错误?

另外,我知道我的帖子很垃圾。在此之后我将删除我的帐户,以减轻社区的压力。

一些更新:

查看我的调试日志输出,我看到:

[Time: ][Thread: 883837][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:99] [Method: manageFileLifecycle.sh] - FILE_DATESTAMP -> 2023-05-07 03:20:29.000000000 -0400 [Time: ][Thread: 883837][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1683444029

显示的文件早于 6 个月,但不是由 >= 删除子句触发的。不确定我做错了什么

对于@markp-fuso:

declare -- FILE_TIMESTAMP="1688178017"
declare -i EARLIEST_DELETE_DATE="1697037095"
declare -i EARLIEST_COMPRESS_DATE="1698419495"
FILE_TO_PROCESS -> /opt/cws/log/S93206319.log, FILE_DATESTAMP -> 2023-06-30 22:20:17.000000000 -0400 FILE_TIMESTAMP -> 1688178017, EARLIEST_DELETE_DATE -> 1697037095, EARLIEST_COMPRESS_DATE -> 1698419495

在日志输出中,我看到该文件被跳过,即使它不应该被跳过。

另外,我必须道歉 - 我无法分享整个脚本,因为它是公司财产=(

更多信息:

提取的特定文件不起作用:

declare -- FILE_TIMESTAMP="1679107439"
declare -i EARLIEST_DELETE_DATE="1697040305"
declare -i EARLIEST_COMPRESS_DATE="1698422705"
FILE_TO_PROCESS -> /opt/cws/log/S93382366.log, FILE_DATESTAMP -> 2023-03-17 22:43:59.000000000 -0400 FILE_TIMESTAMP -> 1679107439, EARLIEST_DELETE_DATE -> 1697040305, EARLIEST_COMPRESS_DATE -> 1698422705
file skipped as it wasnt in teh date ranges

[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:93] [Method: manageFileLifecycle.sh] - processRequestedFile -> enter
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:94] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S93382366.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:95] [Method: manageFileLifecycle.sh] - ERROR_COUNT -> 0
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:96] [Method: manageFileLifecycle.sh] - FILE_TO_PROCESS -> /opt/cws/log/S93382366.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:97] [Method: manageFileLifecycle.sh] - FILE_CORE_NAME -> S93382366
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:98] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT -> log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:99] [Method: manageFileLifecycle.sh] - FILE_DATESTAMP -> 2023-03-17 22:43:59.000000000 -0400
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1679107439
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:107] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT and FILE_CORE_NAME do not match - executing with a file extension
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:139] [Method: manageFileLifecycle.sh] - File has extension log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:298] [Method: manageFileLifecycle.sh] - RETURN_CODE -> 0
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:299] [Method: manageFileLifecycle.sh] - processRequestedFile -> exit

似乎至少命中了删除子句的文件:

排版输出:

declare -- FILE_TIMESTAMP="1703308529"
declare -i EARLIEST_DELETE_DATE="1697040305"
declare -i EARLIEST_COMPRESS_DATE="1698422705"
FILE_TO_PROCESS -> /opt/cws/log/S20504215.log, FILE_DATESTAMP -> 2023-12-23 00:15:29.000000000 -0500 FILE_TIMESTAMP -> 1703308529, EARLIEST_DELETE_DATE -> 1697040305, EARLIEST_COMPRESS_DATE -> 1698422705

调试日志的输出:

[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:93] [Method: manageFileLifecycle.sh] - processRequestedFile -> enter
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:94] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:95] [Method: manageFileLifecycle.sh] - ERROR_COUNT -> 0
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:96] [Method: manageFileLifecycle.sh] - FILE_TO_PROCESS -> /opt/cws/log/S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:97] [Method: manageFileLifecycle.sh] - FILE_CORE_NAME -> S20504215
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:98] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT -> log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:99] [Method: manageFileLifecycle.sh] - FILE_DATESTAMP -> 2023-12-23 00:15:29.000000000 -0500
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1703308529
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:107] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT and FILE_CORE_NAME do not match - executing with a file extension
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:139] [Method: manageFileLifecycle.sh] - File has extension log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:143] [Method: manageFileLifecycle.sh] - File timestamp is greater than or equal to the delete date. File -> /opt/cws/log/S20504215.log, Timestamp -> 1703308529, EARLIEST_DELETE_DATE -> 1697035331
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:148] [Method: manageFileLifecycle.sh] - Deleting file /opt/cws/log/S20504215.log as it falls after the earliest delete date range.
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:149] [Method: manageFileLifecycle.sh] - EXEC: deleteRequestedFile /opt/cws/log/S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:462] [Method: manageFileLifecycle.sh] - deleteRequestedFile -> enter
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:463] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:464] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE -> /opt/cws/log/S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:465] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE_NAME -> S20504215.log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:466] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE_PATH -> /opt/cws/log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:470] [Method: manageFileLifecycle.sh] - Checking if /opt/cws/log exists...
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:516] [Method: manageFileLifecycle.sh] - Returning to previous directory /opt/cws/log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:520] [Method: manageFileLifecycle.sh] - Previous directory: /opt/cws/log, New working directory: /opt/cws/log
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:530] [Method: manageFileLifecycle.sh] - RETURN_CODE -> 1
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:531] [Method: manageFileLifecycle.sh] - deleteRequestedFile -> exit

目标是删除距离当前日期超过 30 天的任何文件,压缩距离当前日期超过 14 天但不超过当前日期 30 天的任何文件(这些条目将属于删除范围)以及小于 14 天的任何文件日子就这样被留下了。

答案1

你的逻辑搞反了。你正在治疗那些时间戳就好像他们是年龄。应该:

if (( FILE_TIMESTAMP < EARLIEST_DELETE_DATE ))
then
    delete file
elif (( FILE_TIMESTAMP < EARLIEST_COMPRESS_DATE ))
then
    compress file
else
    echo "file skipped as it wasn't in the date ranges"
fi

您还可以使用以下命令进行便携式操作find

find -- "$file" -prune -mtime +29 -exec rm -f -- {} + -o \
  -mtime +13 -exec gzip -- {} + -o \
  -exec echo Skipping {} +

请注意,尽管有,但对于以(或为、、 )开头--的值,该方法会失败。调用的文件也有问题(它将其视为标准输入;对于 GNU 也是如此)。$file-()!gzip-stat

-mtime +29对于向下舍入为整数天且严格大于 29 天的文件,即 30 天或更早的文件,情况如此。

对于findBSD,这两个问题/混乱都可以通过执行以下操作来解决:

find -f "$file" -prune -mtime +30d -exec rm -f -- {} + -o \
  -mtime +14d -exec gzip -- {} + -o \
  -exec echo Skipping {} +

(注意d后缀)。

虽然使用最新版本的 GNU find,人们可以这样做:

find  -files0-from <(printf '%s\0' "$file") -prune \
  ! -newermt '30 days ago' -exec rm -f -- {} + -o \
  ! -newermt '14 days ago' -exec gzip -- {} + -o \
  -printf 'Skipping %p\n'

不是用户-delete按照暗示代替-exec rm -f {} +,并且不要在这些命令的前 2秒中使用代替,因为命令的成功/失败将在逻辑中使用。-delete-depth-exec ... ';'-exec ... +-execfind

所有这些命令都可以处理多个文件(对于 BSD find,请使用-f "$file1" -f "$file2"...)。

答案2

在问题的最后我们有以下规则:

  • 删除距离当前日期超过 30 天的所有文件
  • 压缩距离当前日期超过 14 天但不超过当前日期 30 天的任何文件(这些条目将属于删除范围)
  • 任何少于 14 天的文件都会被保留

从最新的更新中,我们得到了两组纪元值:

filename: /opt/cws/log/S93382366.log

        FILE_TIMESTAMP="1679107439"      # Fri Mar 17 21:43:59 CDT 2023
  EARLIEST_DELETE_DATE="1697040305"      # Wed Oct 11 11:05:05 CDT 2023
EARLIEST_COMPRESS_DATE="1698422705"      # Fri Oct 27 11:05:05 CDT 2023

    code result: ISN'T working (assuming this means code generates "file skipped as it wasnt in the date ranges")

expected result: delete file (more than 30 days old; NOTE: compression should have occurred a long time ago)

-----------

filename: /opt/cws/log/S20504215.log

        FILE_TIMESTAMP="1703308529"      # Fri Dec 22 23:15:29 CST 2023
  EARLIEST_DELETE_DATE="1697040305"      # Wed Oct 11 11:05:05 CDT 2023
EARLIEST_COMPRESS_DATE="1698422705"      # Fri Oct 27 11:05:05 CDT 2023

    code result: delete file

expected result: file skipped as it wasn't in the date ranges (less than 14 days old; actually, file has timestamp that is 42 days in the future when today = Nov 10 2023)

代码的主要问题:

  • 一个“旧的”纪元日期实际上是较少的比“更新”的纪元日期所以......
  • 该代码需要测试以下时期较少的超过期望的阈值

更正当前代码以实现 3 条所需规则:

if (( FILE_TIMESTAMP < EARLIEST_DELETE_DATE ))
then
    echo "delete file"
elif (( FILE_TIMESTAMP < EARLIEST_COMPRESS_DATE ))
then
    echo "compress file"
else
    echo "file skipped as it wasn't in the date ranges";
fi

如果我们将其应用到我们的 2 个示例文件/纪元元组中,我们会得到:

filename: /opt/cws/log/S93382366.log

        FILE_TIMESTAMP="1679107439"      # Fri Mar 17 21:43:59 CDT 2023
  EARLIEST_DELETE_DATE="1697040305"      # Wed Oct 11 11:05:05 CDT 2023
EARLIEST_COMPRESS_DATE="1698422705"      # Fri Oct 27 11:05:05 CDT 2023

code result: delete file

-----------

filename: /opt/cws/log/S20504215.log

        FILE_TIMESTAMP="1703308529"      # Fri Dec 22 23:15:29 CST 2023
  EARLIEST_DELETE_DATE="1697040305"      # Wed Oct 11 11:05:05 CDT 2023
EARLIEST_COMPRESS_DATE="1698422705"      # Fri Oct 27 11:05:05 CDT 2023

code result: file skipped as it wasn't in the date ranges

相关内容