Home > database >  check if shell argument is last day of month
check if shell argument is last day of month

Time:02-19

i want to check if the passed argument in command line is the last day of month like below: ./check_date.sh 20210731

if 20210731 is the last day of month echo "last day of month" else echo "no the last day of month"

mydate="$1"
#i found this code in stackoverflow thread but do not now if can take my argument to check weather is the last day or month or not

if [[ $(date -d " 1 day"  %m) != $(date  %m) ]]
then
    echo "Today is the last day of the month"
else
    echo "Today is NOT the last day of the month"
fi


thank you

CodePudding user response:

Just check if 1 day added to your date $1, represented as day of month %d, evaluates to 01:

if [[ $(date -d "$1   1 day"  %d) == 01 ]]
then echo "$1 is the last day of month"
else echo "$1 is NOT the last day of month"
fi
  • Related