How can I get the 1st and last date of the previous month in a Bash script?

后端 未结 7 1674
小蘑菇
小蘑菇 2020-12-31 03:52

I have scheduled a Bash script to run on the 1st of the month but I need to create 2 variables in it with the 1st and last date of the previous month, whatever those may be.

7条回答
  •  再見小時候
    2020-12-31 04:34

    TEST

    first=`date -d "02/05/2018" +"%Y%m01"`
    last=`date -d "02/05/2018 + 1 month-5 day" +"%Y%m%d"`
    

    example

    RUNDATE="20180207"
    y="${RUNDATE:0:4}"
    m="${RUNDATE:4:2}"
    d="${RUNDATE:6:2}"
    RUNDATE_START=`date -d "$m/$d/$y" +"%Y%m01"`
      RUNDATE_END=`date -d "$m/$d/$y + 1 month - $d day" +"%Y%m%d"`
    

    result

    20180201
    20180228

提交回复
热议问题