Parth Patel
Parth Patel

Reputation: 23

Yesterday's date variable in BASH on AIX Server

I want to store yesterday's date in BASH variable to search for yesterday's files with that variable in the file-name wildcard search. I am using the following format for New York City, NY, USA (EST) time zone, and wanted to know whether it is guaranteed to fetch yesterday's date from the system date; else I can make further changes to the variable.

 yesterday=$(TZ=GMT+28 date +%Y%m%d)
 ...
  for file in $HOME_DIR/*$yesterday*.txt;
 ...

The text filename in HOME_DIR would be as follows: "ABC_20171011064612.txt"

update 1: Attempt for removing daylight savings related issues:

yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)"|grep -v $(date +%Y%m%d)|sort|tail -1)

1) Convert two dates to string, 24 hours and 14 (picked arbitrarily to be less than 24 hours) hours before today's date

2) Filter for dates that are not today's date

3) Sort strings from 2) in ascending order

4) Assign yesterday variable to last tail -1 entry of the list

Upvotes: 0

Views: 3394

Answers (2)

Walter A
Walter A

Reputation: 20032

You attempted

yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)|
            grep -v $(date +%Y%m%d)|sort|tail -1)

I think it worked.

Upvotes: 0

tshiono
tshiono

Reputation: 22042

It may not be always right due to DST, although it will not be a big issue. You could rather say:

yesterday=$(date -d yesterday +%Y%m%d)

Upvotes: 1

Related Questions