Count the number of days in the current month
DarkBune
Posted messages
215
Registration date
Status
Member
Last intervention
-
tuxboy Posted messages 1083 Status Member -
tuxboy Posted messages 1083 Status Member -
Hello,
I would like to create a script that will display the number of days in the current month
here is my script
#! /bin/bash
MOIS=$(date +"%m") case $MOIS in 01) NOM="janvier"; 02)
NOM="février"; 03) NOM="mars"; 04) NOM="avril"; 05) NOM="mai"; 06) NOM="juin";
07) NOM="juillet"; 08) NOM="août"; 09) NOM="septembre"; 10) NOM="octobre"; 11) NOM="novembre"; 12) NOM="de$
echo "31 days in "$NOM" "$ANNEE;; 04|06|09|11) echo "30 days in "$NOM" "$ANNEE;;
02) BISSEXTILITE=$(expr $ANNEE %4) if [ $BISSEXTILITE != 0 ] then echo "28 days in "$NOM" "$ANNE
else BISSEXTILITE=$(expr $ANNE %100) if [ $BISSEXTILITE != 0 ] then echo "29 days in "$NOM" "$ANNE
else BISSEXTILITE=$(expr $ANNE %400) if [ $BISSEXTILITE != 0 ] then echo "28 days in "$NOM" "$ANNEE
else echo "29 days in "$NOM" "$ANNEE fi fi fi;; esac
I don't understand, it shows me an error on line 1
Thank you
I would like to create a script that will display the number of days in the current month
here is my script
#! /bin/bash
MOIS=$(date +"%m") case $MOIS in 01) NOM="janvier"; 02)
NOM="février"; 03) NOM="mars"; 04) NOM="avril"; 05) NOM="mai"; 06) NOM="juin";
07) NOM="juillet"; 08) NOM="août"; 09) NOM="septembre"; 10) NOM="octobre"; 11) NOM="novembre"; 12) NOM="de$
echo "31 days in "$NOM" "$ANNEE;; 04|06|09|11) echo "30 days in "$NOM" "$ANNEE;;
02) BISSEXTILITE=$(expr $ANNEE %4) if [ $BISSEXTILITE != 0 ] then echo "28 days in "$NOM" "$ANNE
else BISSEXTILITE=$(expr $ANNE %100) if [ $BISSEXTILITE != 0 ] then echo "29 days in "$NOM" "$ANNE
else BISSEXTILITE=$(expr $ANNE %400) if [ $BISSEXTILITE != 0 ] then echo "28 days in "$NOM" "$ANNEE
else echo "29 days in "$NOM" "$ANNEE fi fi fi;; esac
I don't understand, it shows me an error on line 1
Thank you
6 answers
-
Hello,
In bash, it's much simpler:
The following code:date -d "-$(date +%d) days + 1 month" +%d
displays the number of days in the current month.
The command "date" allows for calculations in natural language, for example, by saying "today + 1 year + 1 month - 3 days."
To display text around this number:date -d "-$(date +%d) days + 1 month" +"%d days in %B"
The command works as follows:
date +%d
returns the current day (14, for October 14).
$(date +%d)
Places the day in a number, a variable.
-$(date +%d) days
subtracts (-) the number (14) of days (days). For today, it would show “-14 days.”
date -d "-$(date +%d) days"
Displays the date corresponding to the current date -14 days. That is the last day of the previous month.
date -d "-$(date +%d) days + 1 month"
Since we want the current month, we need to add a month.
Here we display the complete date of the last day of this month.
If we only want the day (31, for this month), we add +%d:
date -d "-$(date +%d) days + 1 month" +%d
In other programming languages (for example, in JS for me right now), it is common to do the following: take the first day of a given month (October 1), add a month (November 1), and subtract a day (October 31), and you then have the last day of the month. -
Hello,
Does your script appear as is in your text editor?
If you don't use a semicolon (;) between each statement, at least include a line break, if only for readability ;-(
--
_______________________________ ☯ Zen my nuggets ☮ ______________________________
Do something for the environment, close your windows and adopt a penguin… 🐧 -
-
-
Here is the answer to your question.
However, I would like to be able to enter a date and a month in order to get a response.
Can you help me?
#!/bin/bash
MONTH=$(date +"%m")
case $MONTH in
01) NAME="january";;
02) NAME="february";;
03) NAME="march";;
04) NAME="april";;
05) NAME="may";;
06) NAME="june";;
07) NAME="july";;
08) NAME="august";;
09) NAME="september";;
10) NAME="october";;
11) NAME="november";;
12) NAME="december";;
esac
YEAR=$(date +"%Y")
case $MONTH in
01|03|05|07|08|10|12)
echo "31 days in $NAME $YEAR";;
04|06|09|11)
echo "30 days in "$NAME" "$YEAR;;
02)
LEAPYEAR=$(expr $YEAR % 4)
if [ $LEAPYEAR != 0 ]
then
echo "28 days in "$NAME" "$YEAR
else
LEAPYEAR=$(expr $YEAR % 100)
if [ $LEAPYEAR != 0 ]
then
echo "29 days in "$NAME" "$YEAR
else
LEAPYEAR=$(expr $YEAR % 400)
if [ $LEAPYEAR != 0 ]
then
echo "28 days in "$NAME" "$YEAR
else
echo "29 days in "$NAME" "$YEAR
fi
fi
fi
esac
THANK YOU IN ADVANCE -
Hello,
Just to make a different suggestion, you can try this:echo $(cal) | awk '{print $NF}'