0

I am writing a script a check whether exist a backup file every morning Monday to Friday. These backup file is saved in the end of everyday from Monday to Friday only named as

02_10_15

There is a problem that if I run my script on Monday said

09_10_15

It won't find out the file because the file name is

06_10_15

rather than yesterday

08_10_15

Please find my date code below,

#Create variables
yday=$(date --date yesterday +"%d_%m_%y")
#yday="02_10_15"
FileName=$(date --date yesterday +"%Y%m%d")

How can I get the date for Monday special for last Friday.

2 Answers 2

1
unset mon
[ $(date +%u) -eq 1 ] && mon='Fri'
yday=$(date --date="last ${mon}day" +"%d_%m_%y")

So if it is Monday date +%u produce 1 so mon variable will set to Fri and --date's string will be last Friday. Otherways it will be last day as mon remain unset(empty)

2
  • Could you please elaborate on how your snippet works? Commented Nov 9, 2015 at 10:18
  • Thank you @Costas . Your idea is pretty good but your the second line didn't work for me. I have changed them a little bit. Please find the code below: if test $(date +"%u") -eq 1 then mon='Fri' fi yday=$(date --date="last ${mon}day" +"%d_%m_%y") Commented Nov 9, 2015 at 10:51
1

According to the info page, GNU date accepts other forms which you can use. Here is an example:

FileName=$(date --date '1 day ago' +"%Y%m%d")
FileName=$(date --date 'last Friday' +"%Y%m%d")

However, it does not appear to support variants such as "last weekday" or "last day of week". If your script has to run only on week-days (Monday through Friday), or take account of holidays, it will need more than a single line to accomplish this.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.