Date command

Just writing a script as part of a backup procedure to use a data file 7 days previous if there has not been one sent over.

Got stuck on trying to get the date command to insert the date from 7 days ago i.e. todays date is 12/09/10 7 days ago would be 12/09/03

I would want the date command to insert 12/09/03

Try something like this..

$date --date="1 week ago"
Mon Sep  3 07:45:47 EDT 2012

$date -d'1 week ago' 
Mon Sep  3 07:46:05 EDT 2012

$date
Mon Sep 10 07:46:26 EDT 2012
1 Like

Above commands works if you have GNU Date command.. If you dont have GNU date then you might have to mess with TZ.. something like below :slight_smile:

 
gold0813*TPCARCST-/wload/clht/home/clhtora>date
Mon 10 Sep 12:52:25 2012
gold0813*TPCARCST-/wload/clht/home/clhtora>echo $TZ
GMT0BST,M3.5.0/1,M10.5.0/2
gold0813*TPCARCST-/wload/clht/home/clhtora>TZ=TZ+168
gold0813*TPCARCST-/wload/clht/home/clhtora>date
Mon  3 Sep 11:52:34 2012

1 Like

script doesn't seem to work, not sure why though, any idea? :slight_smile:

 
#!/bin/bash
#A backup script if the default data file has not been sent over
#
EXTEN=.txt
VALID=VSL0120
DATE1=$(date +%y%m%d)
DATE2=000000
DATE3='TZ=GMT-168 $date +%y%m%d'
DIR=/apps/live/aahvanroutedata
#
if [ ! $DIR/VSL01_D* ]
#
then
touch /export/home/tjmoore/default_data_backup_email < "We have not rece
ived todays default data file from Alliance, the default data file has b
een used from the previous weeks same day"
#
cp $DIR/$VALID$DATE3$DATE2$EXTEN $DIR/$VALID$DATE1$DATE2$EXTEN 2&1 >>
/export/home/tjmoore/default_data_backup_email
#
/usr/local/bds/mailsend.s mailx "Backup Default Data File has been used"
<email address> j<email address> /export/home/tjmoore/de
fault_data_backup_email > 2&1>/dev/null
#
fi

Any errors ??

no errors are popping up, appreciate theres not a lot to work off, the file I try to create in the first section isnt being created either, just thinking does the test I do at the start mean if that file does not exist then carry on with the rest of the script?

below 2 statements are at fault

 
DATE3='TZ=GMT-168 $date +%y%m%d'

TZ=TZ+168 to go back 7 days

 if [ ! $DIR/VSL01_D* ] 

ir should be

 if [ ! -e ...... ]

This might be the cause of error... please check..

$ DATE3='TZ=GMT-168 $date +%y%m%d'
$ echo $DATE3
TZ=GMT-168 $date +%y%m%d

Thankyou, getting somehwere now, just the DATE3 variable which isn't working correctly, I have the below now from what I understood from your reply. I need the date to be written into where the variable lies in the script e.g. 120910

 
#!/bin/bash
#A backup script if the default data file has not been sent over
#
EXTEN=.txt
VALID=VSL0120
DATE1=$(date +%y%m%d)
DATE2=000000
DATE3='TZ=TZ+168 $date +%y%m%d'
DIR=/apps/live/aahvanroutedata
#
if [ ! -e $DIR/VSL01_D* ]
#
then
        touch /export/home/tjmoore/default_data_backup_email < cat "We have not
        received todays default data file from Alliance, the default data file h
        as been used from the previous weeks same day"    
#
        cp $DIR/$VALID$DATE3$DATE2$EXTEN $DIR/$VALID$DATE1$DATE2$EXTEN 1 >>
        /export/home/tjmoore/default_data_backup_email
#
        /usr/local/bds/mailsend.s mailx "Backup Default Data File has been used"
        <email address> <email address> /export/home/tjmoore/de
        fault_data_backup_email > 2&1>/dev/null
#
fi

you shoud re write it as below

DATE3='TZ=TZ+168 $date +%y%m%d'

To

 TZ=TZ+168
DATE3=`date +%y%m%d'`
TZ=TZ-168 

just in case if you use date somewhere down below in the script.

Doesnt seem to want to run the date command in between the TZ alterations

Just comes back with this if you echo it

 
date +%y%m%d'

If you run it like this however it does work but the the TZ changes dont apply

 
DATE=$(date +%y%m%d)

hope this will give you a clear idea... I missed one single quote in the above command..

wload/lscp/home> date
Mon 10 Sep 16:29:20 2012
wload/lscp/home> TZ=TZ+168
wload/lscp/home> DATE3=`date +'%y%m%d'`
wload/lscp/home> TZ=TZ
wload/lscp/home> echo $DATE3
120903
wload/lscp/home> date
Mon 10 Sep 16:29:34 2012
 

should be:

        /usr/local/bds/mailsend.s mailx "Backup Default Data File has been used"
        <email address> <email address> /export/home/tjmoore/de
        fault_data_backup_email 2>&1 >/dev/null

IF you want STDERR to terminal, and STDOUT to /dev/null (as far as I know your code is not correct there).

And some line above you have 1 >> , I think it should be 1>> without space (or take away the "1", "1" (STDOUT) are default when using >>

One more thing, most people are not using "#" for empty lines, normally they are just empty, only a tip.

1 Like

just wrote a seperate script as it seems to be just the date command thats not working now, I have the below but it doesnt output the date

#!/bin/bash
#
TZ=TZ+168
DATE='date +'%y%m%d''
TZ=TZ-168
#
echo $DATE

it just outputs

date +%y%m%d

Try this..

$ DATE=`date +'%y%m%d'`
$ echo $DATE
120911

or another one

$ DATE=$(date +'%y%m%d')
$ echo $DATE
120911

Tried both methods, using brackets does seem to make the date command work however the TZ changes don't apply to it so its just outputs todays date

Hi JayJay02,

Can you try the below command?

DATE=`TZ=GMT-168 date +%y%m%d` 

This yielded me

sirahu@sirahu-dt037:~$ DATE=`TZ=GMT-168 date +%y%m%d` ;echo $DATE
120912

where

sirahu@sirahu-dt037:~$ date
Tue Sep 11 17:25:45 IST 2012

All of the above just give todays date, I need it to be seven days previous

If you have FreeBSD try

date -v-7d +"%y%m%d"

Adjust the format to what you want.

1 Like

Sorry I run Solaris, so the above won't work, thats for the input though :slight_smile: