Compare Date to today's date in shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Compare Date to today's date in shell script
# 1  
Old 02-10-2020
Compare Date to today's date in shell script

Hi Community!

Following on from this code in another thread:

Code:
#!/bin/bash
file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'`
file_date=`/bin/date -d "$file_string"`
file_epoch=`/bin/date -d "$file_string" +%s`
now_epoch=`/bin/date +%s`

if [ "$file_epoch" -gt "$now_epoch" ]
then
        #let difference=$file_epoch-$now_epoch
       difference=`/usr/bin/expr $file_epoch - $now_epoch`
elif [ "$now_epoch" -gt "$file_epoch" ]
then
       #let difference=$now_epoch-$file_epoch
      difference=`/usr/bin/expr $now_epoch - $file_epoch`
else
        let difference=0
fi

if [ "$difference" -ge "172800" ]
then
        echo "More than 2 days between $file_date and now"
else
        echo "Less than 2 days between $file_date and now"
fi

The code above works with the following input file:

Code:
$ cat date.txt
Not After : Jul 28 14:09:57 2017 GMT
$ ./script.sh
More than 2 days between Fri 28 Jul 15:09:57 BST 2017 and now
$

But my target file just contains a date such as "10/02/2020"

I'm looking for a way to check if dates listed within a file are greater than 1 year.

The thread above is very close but the date format is not my use case.

What is the best way to achieve this?

Thanks in advance!
# 2  
Old 02-10-2020
You should not need awk (or any command line text processor) to convert from various date formats to unixtime (epoch time) and back again.

I spent a few hours today on MQTT with various date and time formats, passing between PHP and Node.js (Javascript) and back, converting unixtime to various formatted date strings and back.

I did not require any text processing software to do this.

Working with dates and time is very standard these days, and we generally do not need text processors to format date and time strings, convert between various formats, and back. This is all "standard stuff" in computer science for quite some time.
# 3  
Old 02-10-2020
Unfortunately, date does not immediately recognize your format. Depending on your bash and date versions, the request can be fulfilled in two - admittedly a bit complex - lines:


Code:
readarray -td/ DATARR <<< "10/02/2020"
if (( ( ( $(date +%s) - $(date -d"${DATARR[2]}/${DATARR[1]}/${DATARR[0]}" +%s)  ) / 86400 ) > 2 )); then printf "greater than 2 days\n"; else printf "less or equal 2 days\n"; fi
 less or equal 2 days

The readarr builtin can read your file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

2. Shell Programming and Scripting

Need to add a date column (today's date) in file

Hi I have file with number status and date1 and date1 field, want add a column today between column date1 and date2. file1.txt number status date1 date2 ===== ==== === ===== 34567 open 27/06/13 28/06/13 45678 open 27/06/13 28/06/13 43567 open 27/06/13 28/06/13 ... (1 Reply)
Discussion started by: vijay_rajni
1 Replies

3. Shell Programming and Scripting

UNIX date fuction - how to deduct days from today's date

Hi, One of my Unix scripts needs to look for files coming in on Fridays. This script runs on Mondays. $date +"%y%m%d" will give me today's date. How can I get previous Friday's date.. can I do "today's date minus 3 days" to get Friday's date? If not, then any other way?? Name of the files is... (4 Replies)
Discussion started by: juzz4fun
4 Replies

4. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

5. Shell Programming and Scripting

Shell Scripting: Copy Files with Today's date

I was wondering the best way about finding files that were created today and copy them to a directory (grep ?). There can be multiple files for todays date or none. I am looking to copy all of the .lis files for todays date. I may need to modify the filename to include todays date but for the... (4 Replies)
Discussion started by: smkremer
4 Replies

6. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

7. Shell Programming and Scripting

Shell Script to compare files, check current date and count

Hello - I have written the following basic shell script to count files, compare files and look for a particular strings in a file. Problem 1: How do I define more than 1 file location? #!/bin/bash #this is a test script FILES=$(ls /home/student/bin/dir1, home/student/bin/dir2)... (0 Replies)
Discussion started by: DallasT
0 Replies

8. Shell Programming and Scripting

Calculate 30/31 days from today date script

Hi Guys, I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own... (3 Replies)
Discussion started by: coolgoose85
3 Replies

9. Shell Programming and Scripting

Past date display ( today - N) in shell

Hi, Any idea to get display of date - n ( n=1,2,3,4 etc) ? For example , dtoudt will easily execute by dtoudt -3 and result is >dtoudt -3 1234852529 Date 2009-2-17 time 14:35:29 day 47 How to get it display in 20090217 instead ? Maybe you have other suggestion to display date... (0 Replies)
Discussion started by: rauphelhunter
0 Replies

10. UNIX for Dummies Questions & Answers

compare today's date with date in a file

Hi I am very new to scripting, Can someone show me how to (in unix shell script) compare the system's date with a date in a file. The requirement is to somehow open this file (which will only have a date in it) and compare it with today's date. If they are equal execute a procedure below but if... (4 Replies)
Discussion started by: siog
4 Replies
Login or Register to Ask a Question