amine jisung
amine jisung

Reputation: 117

check if shell argument is last day of month

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

Upvotes: 1

Views: 554

Answers (1)

pmf
pmf

Reputation: 36088

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

Upvotes: 4

Related Questions