简体   繁体   English

如何在Linux shell脚本或python中找出上周六的日期?

[英]How to find out the date of the last Saturday in Linux shell script or python?

I have python script which i need to run daily for backups. 我有python脚本,我需要每天运行备份。 Now i need to find the date of last saturday because i need that in my script to get the backups i did on last saturdy. 现在我需要找到上个星期六的日期,因为我需要在我的脚本中获取我最后做的备份。 Suppose 假设

on saturday i made this file 星期六我做了这个文件

weekly_user1_Jul-13-2013.sql

I ned to get that name in script which i run daily. 我想在我每天运行的脚本中获取该名称。 so for script running on saturday i need to get todays date , buif its on sunday then i need to get last saturday date. 所以对于在星期六运行的脚本我需要得到今天的日期,在星期天,然后我需要得到最后一个星期六的日期。

how can i do that 我怎样才能做到这一点

$ date +"%b-%d-%Y" -d "last saturday"
Jul-13-2013

In python script: 在python脚本中:

from datetime import date
from datetime import timedelta
today = date.today()
last_saturday = today - timedelta(days= (today.weekday() - 5) % 7)

On Mac OS it would be: 在Mac OS上它将是:

$ date -v -sat +"%b-%d-%Y"
Mar-28-2015

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM