Mohamed Douzi
Mohamed Douzi

Reputation: 61

How to get last Week's date in bash SunOS

Here is my issue: I have a backup bash script that needs to access a folder with a date in its name for example : backup_01072022 .

I used date=`TZ=GMT+24 date +%d%m%Y` when i needed to access the backup folder of yesterday.

Now I want to access the backup folder of last week :

date=`TZ=GMT+168 date +%d%m%Y` , it doesn't work , it show today's date.

I read that TZ doesn't work for a value above +144.

Is there any other way of manipulating dates in SunOS 6.8 ?

Notes :

SunOS 6.8

version of the date util : 8.5

version of bash : 4.1.11(2)-release

Upvotes: 0

Views: 99

Answers (3)

Mohamed Douzi
Mohamed Douzi

Reputation: 61

Thanks to the helpful commments and answers I was able to make it work using :

/usr/gnu/bin/date -d "last week" '+%d%m%Y'

It turns out I was not using the GNU date util until I specified it explicitly, and that's neither --date nor -d was working for me. I still can't figure out what date util I was using by default if not GNU date.

Upvotes: 1

jhnc
jhnc

Reputation: 16662

I seem to recall SunOS comes with Perl, so if you don't have a date that supports --date="...", you should be able to do:

date=$(perl -MPOSIX -e '
    print POSIX::strftime "%d%m%Y", localtime time-(60*60*24*7)
')

Upvotes: 1

markp-fuso
markp-fuso

Reputation: 34244

This'll depend on the version of date on your system.

With GNU date (v 8.26):

$ TZ=GMT date '+%d%m%Y'
06072022                        # today

$ TZ=GMT date '+%d%m%Y' -d 'last week'
29062022

$ TZ=GMT date '+%d%m%Y' -d '7 days ago'
29062022

NOTE: I'll leave it up to OP to determine if the explicit TZ setting should be adjusted (or used at all)

Upvotes: 2

Related Questions