【问题标题】:Date Comparison in Shell Script doesn't workShell 脚本中的日期比较不起作用
【发布时间】:2016-11-27 10:49:24
【问题描述】:

我知道还有一些其他类似主题的问题。但是解决方案对我不起作用。以下代码应比较两个 .csv 文件的时间戳。但是比较不起作用。

#!/bin/bash
input1="/home/seconion/Desktop/DatasetAnalyse/BYOD/alertTimeConverted.csv"
input2="/home/seconion/Desktop/DatasetAnalyse/BYOD/gt_byodTimeConverted.csv"
while IFS=',' read -r timestamp1 msg1 proto1 srcIP1 srcPort1 dstIP1 dstPort1
do
    echo "next alert"
    while IFS=',' read -r timestamp21 timestamp22 duration2 proto2 srcIP2 srcPort2 dstIP2 dstPort2 iflags2 uflags2 riflags2 ruflags2 pkt2 rpkt2 oct2 roct2 app2 entropy2 rentropy2 label2 category12 category22 comments2
    do
        alertTime=$(date -d "$timestamp1" +"%H%M%S%N")
        streamTimeStart=$(date -d "$timestamp21" +"%H%M%S%N")
        streamTimeEnd=$(date -d "$timestamp22" +"%H%M%S%N")

        if [ $streamTimeStart -lt $alertTime ] && [ $alertTime -lt $streamTimeEnd ];
            then echo "true"
        fi
    done < "$input2"
done < "$input1"

【问题讨论】:

  • 您能否告诉我们streamTimeStartstreamTimeEndalertTime 的一些示例值?
  • 这是格式:223445248591000
  • 你用的是GNU date,能查一下date --version吗?
  • 它的 GNU coreutils 8.21
  • 在您的问题中添加来自$timestamp1$timestamp21$timestamp22 的真实示例。

标签: bash shell date


【解决方案1】:

而不是使用这种格式

alertTime=$(date -d "$timestamp1" +"%H%M%S%N")
streamTimeStart=$(date -d "$timestamp21" +"%H%M%S%N")
streamTimeEnd=$(date -d "$timestamp22" +"%H%M%S%N")

使用epoch,它更理智

alertTime=$(date -d "$timestamp1" +"%s")
streamTimeStart=$(date -d "$timestamp21" +"%s")
streamTimeEnd=$(date -d "$timestamp22" +"%s")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多