比较MAC OSX中Bash中的两个日期 [英] Compare two dates in Bash in MAC OSX

查看:54
本文介绍了比较MAC OSX中Bash中的两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Bash的新手,提前致歉.

I'm new to Bash, apologies in advance.

设置

我有一个特定的结束日期 end ,它取决于特定的开始日期 s 和时间段长度 p ,例如,end = s + p .

I have a particular end date end which depends on a particular starting date s and a period length p such that, end=s+p.

问题

仅当今天的日期早于或等于结束日期时,我才想执行命令.

I want to execute a command if and only if today's date is before or equal to the end date.

即:执行命令iff date≤end .

That is: execute command iff date ≤ end.

代码

s='20/09/2017'
p='1'
end=$( date -j -v +"$p"d -f "%d/%m/%Y" "$s")

if [[ date < "$end" ]];
then
   echo 'ok'
fi

有两件事不是应该的样子,

There are 2 things not the way they should be,

  1. p = 1 表示 end = '21/09/2017'< date = '26/09/2017',但我仍然得到 ok .
  2. 我使用 date<"$ end" ,但我要 date≤"$ end"
  1. p=1 implies end = '21/09/2017' < date = '26/09/2017', but still I get an ok.
  2. I use date < "$end" but I want date ≤ "$end"

如何更正1和2?

推荐答案

  1. 您的:

  1. Your:

end=$( date -j -v+"$p"d -f "%d/%m/%Y" "$s" )

无法产生您期望的结果:

does not produce what you expect:

echo "$end"
Thu Sep 21 14:40:54 CEST 2017

您可以通过以下方式获得期望的结果

What you expect can be obtained with:

end=$( date -j -v+"$p"d -f "%d/%m/%Y" "$s" +"%d/%m/%Y" )

  • 您的比较:

  • Your comparison:

    if [[ date < "$end" ]]; then
    

    行为不符合您的想象.调用 date 命令的结果不会扩展 date .因此,您的比较是字符串"date" 和字符串之间的字符串比较(在 [[..]] 条件表达式上下文中为< )"CEST 2017 Thu Sep 21 14:40:54" .您应该使用类似这样的内容:

    does not behave as you think. date is not expanded as the result of the invocation of the date command. So, your comparison is a string comparison (< in the [[.]] conditional expression context) between string "date" and string "Thu Sep 21 14:40:54 CEST 2017". You should use something like:

    date=$( date )
    if [[ "$date" < "$end" ]]; then
    

    代替(但是使用 date 作为变量名可能不是一个好主意).

    instead (but using date as a variable name is probably not a very good idea).

    从其他答案中可以看出,您选择的日期格式不是最佳的比较方式.因此,将各种修补程序与其他明智的建议结合起来,可以尝试执行以下操作:

    As noticed by the other answers, the date format you chose is not the best for comparisons. So, combining the various fixes with the other wise suggestions, you could try something like:

    s='20170920'
    p='1'
    end=$( date -j -v+"$p"d -f "%Y%m%d" "$s" +"%Y%m%d" )
    now=$( date +"%Y%m%d" )
    if (( now <= end )); then
       echo 'ok'
    fi
    

    注意: if((now< = end));那么是一种算术比较,它应该在这种特定情况下有效.还要注意,它使用 now end ,而不是 $ now $ end :((.))算术比较将变量名称解释为存储在变量中的整数值.无论如何,即使使用 YYYYmmdd 格式的日期,也好像它们是整数一样,也不是很干净.使用UNIX时间戳(可能至少在 2038年之前是可以的):>

    Note: if (( now <= end )); then is an arithmetic comparison and it should work in this specific case. Note also that it uses now and end, not $now and $end: the ((.)) arithmetic comparison interprets variable names as the integer value stored in the variable. Anyway, using dates, even in YYYYmmdd format, as if they were integers is not that clean. Using UNIX timestamps, which should be OK at least until year 2038, is probably cleaner:

    s='20170920'
    p='1'
    end=$( date -j -v+"$p"d -f "%Y%m%d" "$s" +"%s" )
    now=$( date +"%s" )
    if (( now <= end )); then
       echo 'ok'
    fi
    

    因为这里 now end 是自

    because here now and end are number of seconds since the Epoch, that is, true integers. Note, however, that the result could be different from that of the previous solution because you have now a one-second accuracy instead of one-day. Chose which one corresponds to your needs.

    这篇关于比较MAC OSX中Bash中的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆