Skip to content

Instantly share code, notes, and snippets.

@adkdev
Created January 18, 2016 08:08
Show Gist options
  • Save adkdev/5c29852b985b7f07d98c to your computer and use it in GitHub Desktop.
Save adkdev/5c29852b985b7f07d98c to your computer and use it in GitHub Desktop.
get previous month in shell script
#!/bin/bash
fn=$1
if [ "$2" == "" ] || [ "$3" == "" ]; then
if [ -f last_${fn}.txt ]; then
value=$(<last_${fn}.txt)
_yy=${value:0:4}
_mm=${value:4:2}
echo "$_yy $_mm"
mm=$(date --date="$(date +$_yy-$_mm-15) -1 month" +"%m")
yy=$(date --date="$(date +$_yy-$_mm-15) -1 month" +"%Y")
echo "$yy $mm"
else
echo 'Nothing to do `year` and `month` are required.'
exit
fi
else
yy=$2
mm=$3
fi
echo $yy$mm > ./last_${fn}.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment