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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With