V
V
Vadim Pirx2017-06-21 09:33:54
bash
Vadim Pirx, 2017-06-21 09:33:54

How to compare dates from file with current date?

Task: It is necessary to drive the dates from the file into a variable line by line, convert the date to seconds and compare with the current date in seconds. If date from file is less than or equal to current date then do somesing..
file with dates of that format

$ cat date_out
2017-06-18 11:02:00
2017-06-17 11:05:00
2017-04-05 17:39:00
2017-03-23 10:46:00
2017-03-21 11:46:00
2017-03-21 11:57:00
2017-03-23 10:49:00

convert current date to seconds
curr_d=`date +%s`

cat date_out | while read start_d
do
    start_d=`date -d $start_d +%s`
    if ['$start_d' -le '$curr_d']
 then do something else

done

err
date: лишний операнд «+%s»

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Pirx, 2017-06-21
@pirxon

As usual, by typing, I picked up quotes and brackets, it worked. this might be useful to someone.

#!/bin/bash
curr_d=`date +%s`
echo "Текущая дата в сек. $curr_d"

cat date_out | while read start_d
do
        start_d=`date -d "$start_d" +%s`
        if (("$start_d" <= "$curr_d"))  
        then
                echo "$start_d меньше либо равна текущей"
        else
                echo "$start_d больше текущей" 
        fi
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question