Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading