0

if there is no argument i can generate the current month and year(012021) and lastmonth(122020) with "-d 'last month'" option. if I entered argument like "01/01/2019" i can handle $currentmonth(012019) but not $pastmonth variable. is there any way to solve this.

#!/bin/bash
if [ -z "$1" ] ;then
currentmonth=`date "+%m%Y"`
pastmonth=`date "+%m%Y" -d 'last month'`
echo $currentmonth
echo $pastmonth
else
currentmonth=`date +%m%Y --date="$1"`
pastmonth=?
echo $currentmonth
echo $pastmonth
fi

1 Answer 1

1

You can do this by “adding” “last month” to the date:

date +%m%Y -d "01/01/2019+last month"

Beware of date’s parsing, for dates with slashes it assumes US format.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.