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 -
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

6 answers

  1. le hollandais volant Posted messages 5294 Status Member 1 059
     
    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.
    4
    1. DarkBune Posted messages 215 Registration date   Status Member Last intervention   9
       
      Hello, thank you for your great explanation

      when I run your script it displays this: "date: incorrect date "- 14 days + 1 month "
      0
      1. le hollandais volant Posted messages 5294 Status Member 1 059 > DarkBune Posted messages 215 Registration date   Status Member Last intervention  
         
        Did you copy and paste or rewrite it?

        In your message, you write "mouth" when it is actually "month".
        0
  2. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 502
     
    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… 🐧
    0
    1. DarkBune Posted messages 215 Registration date   Status Member Last intervention   9
       
      Hi yes, I do it with the command "nano," otherwise did you notice an error? Thank you.
      0
      1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 502 > DarkBune Posted messages 215 Registration date   Status Member Last intervention  
         
        Non because it's unreadable and unpalatable.

        Format it and repost your code between the
        <code>
        </code>
        tags and then we'll decide. ;-\
        0
  3. DarkBune Posted messages 215 Registration date   Status Member Last intervention   9
     


    I have this error.
    0
  4. DarkBune Posted messages 215 Registration date   Status Member Last intervention   9
     


    => my editor
    0
    1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 502
       
      As mentioned above, each instruction must be on a separate line or separated by a semicolon (;)!

      There should be no space between "$" and "(" (source of the 1st error).
      0
  5. djacidj
     
    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
    0
    1. UnGnU Posted messages 1468 Status Contributor 158
       
      Hi,

      You just need to ask for it at the beginning of the script (command "
      read
      ") instead of hardcoding it.
      0
      1. djacidj > UnGnU Posted messages 1468 Status Contributor
         
        Thank you for your response. Could you show me an example as I'm having quite a bit of difficulty with Linux scripting lol

        Thanks in advance!!!
        0
  6. tuxboy Posted messages 1083 Status Member 190
     
    Hello,

    Just to make a different suggestion, you can try this:
    echo $(cal) | awk '{print $NF}'
    0