当前位置:网站首页>Date date calculation in shell script

Date date calculation in shell script

2022-04-23 15:39:00 jack@london

stay shell Script , Sometimes you need to name the file the current date date

#!/bin/sh

DATE=`date +%F`
echo "$DATE"

v_date=`date +%F`
echo "$v_date"

#  Calculate the day before 
v_date_ago_1=`date -d "$v_date -1 day" +%Y-%m-%d`
echo "$v_date_ago_1"

#  Calculate the day before , Transformation for %F Format 
v_date_ago_1=`date -d "$v_date -1 day" +%F`
echo "$v_date_ago_1"

#  Day after calculation 
v_date_future_1=`date -d "$v_date 1 day" +%F`
echo "$v_date_future_1"

be chmod After execution , The outputs are as follows

2022-04-21
2022-04-21
2022-04-20
2022-04-20
2022-04-22

Be careful :shell The equal sign in the script ‘=’ Before and after , No spaces .

This date counts , It can be used to clean up old backup files regularly , For example, delete 7 The day before db Backup file .

OLD_DATE=`date -d "$DATE -7 day" +%F`

rm $BCK_DIR/db_$OLD_DATE.* -f

See

Shell date Detailed command _hrbeuwhw The blog of -CSDN Blog _date shell

版权声明
本文为[jack@london]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231535553805.html