Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Shell Current Date Minus Number of Days

I am new to bash and shell but I am running a debian install and I am trying to make a script which can find a date in the past without having to install any additional packages. From tutorials I have got to this stage:

#!/bin/sh
#
# BACKUP DB TO S3
#

# VARIABLES
TYPE="DATABASE"
DAYS="30"

# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"

# GENERATE PAST DATE FROM DAYS CONTSTANT
OLDERDATE=`expr $CURRENTDATE - $DAYS'

# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py $OLDERDATE

Where I am getting stuck is the fact that my "days" is just the number 30 and isnt datetime formattted, so when I come to minus it from the currentdate variable it obviously isnt compatible.

Would anyone be kind enough to help me find a way to get this working as it should?

like image 418
Jimmy Avatar asked Jan 19 '13 17:01

Jimmy


1 Answers

Try doing this :

#!/bin/sh
#
# BACKUP DB TO S3
#

# VARIABLES
TYPE="DATABASE"
DAYS="30"

# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"

# GENERATE PAST DATE FROM DAYS CONSTANT
OLDERDATE="$(date "+%Y%m%d%H%M%S" -d "$DAYS days ago")"

# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py "$OLDERDATE"

See info coreutils 'date invocation' | less +/28.7\ Relative\ items\ in\ date\ strings

like image 127
Gilles Quenot Avatar answered Oct 27 '22 20:10

Gilles Quenot