0

We use to run shell scripts through Control-M and shells script accepts execution date as input parameter and script should calculate last month's last day whatever order date we run. I am trying to get the same as below but not able to. Please help.

run_date=20210615
startMM_run_date=`date --date="$run_date" '%Y%m01'` --> 20210601

Here how to get "2021/05/31" from $startMM_run_date. Please advice.

Panki
  • 6,221
  • 2
  • 24
  • 33

1 Answers1

2

Assuming GNU date

run_date=20210615

# extract the year and month, and set the day as 01, then subtract 1 day
endOfLastMonth=$(date -d "${run_date:0:4}-${run_date:4:2}-01 - 1 day" "+%Y-%m-%d")
glenn jackman
  • 84,176
  • 15
  • 116
  • 168