Created
January 18, 2016 08:08
-
-
Save adkdev/5c29852b985b7f07d98c to your computer and use it in GitHub Desktop.
get previous month in shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
ref: http://stackoverflow.com/questions/13168463/using-date-command-to-get-previous-current-and-next-month