Last Working Day of the Month

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2














#!/bin/bash
#!/usr/local/bin
#!/usr/sbin
#!/usr/bin
# Script to Check last working Day of the Month

echo " Enter Month and Year :"
read mon year
cal $mon $year| egrep "28|29|30|31"|awk 'BEGIN
var1=$NF;var2=NF;


if (NF > 1 && NF < 7)
val=$NF;
else if (NF == 1)
val=$NF-2;
else if (NF == 7)
val=$NF-1;


print "Last Working Date is : " val;
'


Script Output :



511@ubuntu:~/Unix$ ./test.sh
Enter Month and Year :
4 2015
Last Working Date is : 30
511@ubuntu:~/Unix$ ./test.sh
Enter Month and Year :
5 2015
Last Working Date is : 29
Last Working Date is : 29
511@ubuntu:~/Unix$ ./test.sh
Enter Month and Year :
7 2015
Last Working Date is : 31
511@ubuntu:~/Unix$ ./test.sh
Enter Month and Year :
1 2015
Last Working Date is : 30


Can someone please help why script print twice while we gives a input as below :



511@ubuntu:~/Unix$ ./test.sh
Enter Month and Year :
5 2015
Last Working Date is : 29
Last Working Date is : 29









share|improve this question




























    2














    #!/bin/bash
    #!/usr/local/bin
    #!/usr/sbin
    #!/usr/bin
    # Script to Check last working Day of the Month

    echo " Enter Month and Year :"
    read mon year
    cal $mon $year| egrep "28|29|30|31"|awk 'BEGIN
    var1=$NF;var2=NF;


    if (NF > 1 && NF < 7)
    val=$NF;
    else if (NF == 1)
    val=$NF-2;
    else if (NF == 7)
    val=$NF-1;


    print "Last Working Date is : " val;
    '


    Script Output :



    511@ubuntu:~/Unix$ ./test.sh
    Enter Month and Year :
    4 2015
    Last Working Date is : 30
    511@ubuntu:~/Unix$ ./test.sh
    Enter Month and Year :
    5 2015
    Last Working Date is : 29
    Last Working Date is : 29
    511@ubuntu:~/Unix$ ./test.sh
    Enter Month and Year :
    7 2015
    Last Working Date is : 31
    511@ubuntu:~/Unix$ ./test.sh
    Enter Month and Year :
    1 2015
    Last Working Date is : 30


    Can someone please help why script print twice while we gives a input as below :



    511@ubuntu:~/Unix$ ./test.sh
    Enter Month and Year :
    5 2015
    Last Working Date is : 29
    Last Working Date is : 29









    share|improve this question


























      2












      2








      2







      #!/bin/bash
      #!/usr/local/bin
      #!/usr/sbin
      #!/usr/bin
      # Script to Check last working Day of the Month

      echo " Enter Month and Year :"
      read mon year
      cal $mon $year| egrep "28|29|30|31"|awk 'BEGIN
      var1=$NF;var2=NF;


      if (NF > 1 && NF < 7)
      val=$NF;
      else if (NF == 1)
      val=$NF-2;
      else if (NF == 7)
      val=$NF-1;


      print "Last Working Date is : " val;
      '


      Script Output :



      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      4 2015
      Last Working Date is : 30
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      5 2015
      Last Working Date is : 29
      Last Working Date is : 29
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      7 2015
      Last Working Date is : 31
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      1 2015
      Last Working Date is : 30


      Can someone please help why script print twice while we gives a input as below :



      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      5 2015
      Last Working Date is : 29
      Last Working Date is : 29









      share|improve this question















      #!/bin/bash
      #!/usr/local/bin
      #!/usr/sbin
      #!/usr/bin
      # Script to Check last working Day of the Month

      echo " Enter Month and Year :"
      read mon year
      cal $mon $year| egrep "28|29|30|31"|awk 'BEGIN
      var1=$NF;var2=NF;


      if (NF > 1 && NF < 7)
      val=$NF;
      else if (NF == 1)
      val=$NF-2;
      else if (NF == 7)
      val=$NF-1;


      print "Last Working Date is : " val;
      '


      Script Output :



      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      4 2015
      Last Working Date is : 30
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      5 2015
      Last Working Date is : 29
      Last Working Date is : 29
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      7 2015
      Last Working Date is : 31
      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      1 2015
      Last Working Date is : 30


      Can someone please help why script print twice while we gives a input as below :



      511@ubuntu:~/Unix$ ./test.sh
      Enter Month and Year :
      5 2015
      Last Working Date is : 29
      Last Working Date is : 29






      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 22 '15 at 16:15









      celtschk

      6,58611118




      6,58611118










      asked Aug 22 '15 at 16:11









      Aashish Raj

      4016




      4016




















          6 Answers
          6






          active

          oldest

          votes


















          1














          The problem is your egrep search is going to call awk once for every line that it sees a "28", "29", "30", or "31". Months where the 28th lands before the last calendar week will have awk called twice, since two lines match your search criteria



          You want to always use the second line of the egrep search, so you can use a tail command to only see the last line:



          #!/bin/bash
          #!/usr/local/bin
          #!/usr/sbin
          #!/usr/bin
          # Script to Check last working Day of the Month

          echo " Enter Month and Year :"
          read mon year
          cal $mon $year| egrep "28|29|30|31"| tail -n 1 |awk 'BEGIN
          var1=$NF;var2=NF;


          if (NF > 1 && NF < 7)
          val=$NF;
          else if (NF == 1)
          val=$NF-2;
          else if (NF == 7)
          val=$NF-1;


          print "Last Working Date is : " val;
          '





          share|improve this answer




















          • You are right rexroni.
            – jimmij
            Aug 22 '15 at 17:30










          • Thanks Rexroni.. I assume something wrong with conditions :)
            – Aashish Raj
            Aug 22 '15 at 18:03


















          1














          With date it is only a two line bash calculation:



          #!/bin/bash

          month="$1"
          year="$2"

          read -r dow day < <(date -d "$year/$month/1 +1 month -1 day" "+%u %d")

          echo "Last working day of the month = $(( day - ( (dow>5)?(dow-5):0 ) ))"


          The math is: if dow (the day of the week) is bigger than 5, subtract (dow-5) from the day, else, leave day unchanged.



          Use as:



          $ ./script 2 2015
          Last working day of the month = 27





          share|improve this answer




























            1














            With ksh93:



            $ printf "%(%A %B %d)Tn" "October 2018 last working day"
            Wednesday October 31





            share|improve this answer




























              0














              Your script relies on the screen output of cal. To do things in a more proper way you could use the date command:



              #!/bin/bash
              # Calculates the last working day (Mon-Fri) of the month

              echo "Enter Month and Year:"
              read month year
              lastdom=`date -d "$year/$month/1 + 1 month - 1 day" "+%d"`
              dow=`date -d "$year/$month/1 + 1 month - 1 day" "+%u"`

              if [ $dow -ge 6 ]
              then
              lastwdom=$(($lastdom - $dow + 5))
              else
              lastwdom=$(($lastdom))
              fi

              echo "Last working day is $lastwdom"





              share|improve this answer




























                0














                Your script repeats the print because awk is receiving two lines from egrep. But that has already been covered in other answers.



                I want to explain some alternative way to solve the problem, shorter, easier.



                The program cal could print the week starting on monday (which simplifies the math) when called as this cal -NMC month year. Using that:



                #!/bin/bash

                lastday()
                printf 'Last Working Date of %s/%s = ' "$1" "$2";
                cal -NMC "$1" "$2"

                mon="$1"
                year="$2"
                lastday "$mon" "$year"


                Description:



                /[0-9]+/ Select lines with numbers (avoid empty line).



                NF>5?5:NF Math: If more fields than 5 result is 5, else NF.



                val=$( ... ) Select the value of the field.



                END print val ' Only print the value of the last line (line with numbers).



                Call it like this:



                $ ./test.sh 4 2015
                Last Working Date of 4/2015 = 30
                $ ./test.sh 5 2015
                Last Working Date of 5/2015 = 29
                $ ./test.sh 7 2015
                Last Working Date of 7/2015 = 31
                $ ./test.sh 1 2015
                Last Working Date of 1/2015 = 30
                $ ./test.sh 9 2015
                Last Working Date of 9/2015 = 30
                $





                share|improve this answer






























                  0














                  read -p "Please enter date 'yyyymmdd':" dat
                  yy=`echo $dat|cut -c 1-4`
                  mm=`echo $dat|cut -c 5-6`

                  case $mm in

                  01|02|03)
                  mm=12
                  yy=`expr $yy - 1`
                  dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                  echo $yy$mm$dd
                  ;;

                  04|05|06)
                  mm=03
                  dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                  echo $yy$mm$dd
                  ;;

                  07|08|09)
                  mm=06
                  dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                  echo $yy$mm$dd
                  ;;

                  10|11|12)
                  mm=09
                  dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                  echo $yy$mm$dd
                  ;;

                  *) echo "Invalid month"
                  exit;;
                  esac





                  share|improve this answer






















                    Your Answer








                    StackExchange.ready(function()
                    var channelOptions =
                    tags: "".split(" "),
                    id: "106"
                    ;
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function()
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled)
                    StackExchange.using("snippets", function()
                    createEditor();
                    );

                    else
                    createEditor();

                    );

                    function createEditor()
                    StackExchange.prepareEditor(
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader:
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    ,
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    );



                    );













                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f224808%2flast-working-day-of-the-month%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    1














                    The problem is your egrep search is going to call awk once for every line that it sees a "28", "29", "30", or "31". Months where the 28th lands before the last calendar week will have awk called twice, since two lines match your search criteria



                    You want to always use the second line of the egrep search, so you can use a tail command to only see the last line:



                    #!/bin/bash
                    #!/usr/local/bin
                    #!/usr/sbin
                    #!/usr/bin
                    # Script to Check last working Day of the Month

                    echo " Enter Month and Year :"
                    read mon year
                    cal $mon $year| egrep "28|29|30|31"| tail -n 1 |awk 'BEGIN
                    var1=$NF;var2=NF;


                    if (NF > 1 && NF < 7)
                    val=$NF;
                    else if (NF == 1)
                    val=$NF-2;
                    else if (NF == 7)
                    val=$NF-1;


                    print "Last Working Date is : " val;
                    '





                    share|improve this answer




















                    • You are right rexroni.
                      – jimmij
                      Aug 22 '15 at 17:30










                    • Thanks Rexroni.. I assume something wrong with conditions :)
                      – Aashish Raj
                      Aug 22 '15 at 18:03















                    1














                    The problem is your egrep search is going to call awk once for every line that it sees a "28", "29", "30", or "31". Months where the 28th lands before the last calendar week will have awk called twice, since two lines match your search criteria



                    You want to always use the second line of the egrep search, so you can use a tail command to only see the last line:



                    #!/bin/bash
                    #!/usr/local/bin
                    #!/usr/sbin
                    #!/usr/bin
                    # Script to Check last working Day of the Month

                    echo " Enter Month and Year :"
                    read mon year
                    cal $mon $year| egrep "28|29|30|31"| tail -n 1 |awk 'BEGIN
                    var1=$NF;var2=NF;


                    if (NF > 1 && NF < 7)
                    val=$NF;
                    else if (NF == 1)
                    val=$NF-2;
                    else if (NF == 7)
                    val=$NF-1;


                    print "Last Working Date is : " val;
                    '





                    share|improve this answer




















                    • You are right rexroni.
                      – jimmij
                      Aug 22 '15 at 17:30










                    • Thanks Rexroni.. I assume something wrong with conditions :)
                      – Aashish Raj
                      Aug 22 '15 at 18:03













                    1












                    1








                    1






                    The problem is your egrep search is going to call awk once for every line that it sees a "28", "29", "30", or "31". Months where the 28th lands before the last calendar week will have awk called twice, since two lines match your search criteria



                    You want to always use the second line of the egrep search, so you can use a tail command to only see the last line:



                    #!/bin/bash
                    #!/usr/local/bin
                    #!/usr/sbin
                    #!/usr/bin
                    # Script to Check last working Day of the Month

                    echo " Enter Month and Year :"
                    read mon year
                    cal $mon $year| egrep "28|29|30|31"| tail -n 1 |awk 'BEGIN
                    var1=$NF;var2=NF;


                    if (NF > 1 && NF < 7)
                    val=$NF;
                    else if (NF == 1)
                    val=$NF-2;
                    else if (NF == 7)
                    val=$NF-1;


                    print "Last Working Date is : " val;
                    '





                    share|improve this answer












                    The problem is your egrep search is going to call awk once for every line that it sees a "28", "29", "30", or "31". Months where the 28th lands before the last calendar week will have awk called twice, since two lines match your search criteria



                    You want to always use the second line of the egrep search, so you can use a tail command to only see the last line:



                    #!/bin/bash
                    #!/usr/local/bin
                    #!/usr/sbin
                    #!/usr/bin
                    # Script to Check last working Day of the Month

                    echo " Enter Month and Year :"
                    read mon year
                    cal $mon $year| egrep "28|29|30|31"| tail -n 1 |awk 'BEGIN
                    var1=$NF;var2=NF;


                    if (NF > 1 && NF < 7)
                    val=$NF;
                    else if (NF == 1)
                    val=$NF-2;
                    else if (NF == 7)
                    val=$NF-1;


                    print "Last Working Date is : " val;
                    '






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 22 '15 at 16:48









                    rexroni

                    643616




                    643616











                    • You are right rexroni.
                      – jimmij
                      Aug 22 '15 at 17:30










                    • Thanks Rexroni.. I assume something wrong with conditions :)
                      – Aashish Raj
                      Aug 22 '15 at 18:03
















                    • You are right rexroni.
                      – jimmij
                      Aug 22 '15 at 17:30










                    • Thanks Rexroni.. I assume something wrong with conditions :)
                      – Aashish Raj
                      Aug 22 '15 at 18:03















                    You are right rexroni.
                    – jimmij
                    Aug 22 '15 at 17:30




                    You are right rexroni.
                    – jimmij
                    Aug 22 '15 at 17:30












                    Thanks Rexroni.. I assume something wrong with conditions :)
                    – Aashish Raj
                    Aug 22 '15 at 18:03




                    Thanks Rexroni.. I assume something wrong with conditions :)
                    – Aashish Raj
                    Aug 22 '15 at 18:03













                    1














                    With date it is only a two line bash calculation:



                    #!/bin/bash

                    month="$1"
                    year="$2"

                    read -r dow day < <(date -d "$year/$month/1 +1 month -1 day" "+%u %d")

                    echo "Last working day of the month = $(( day - ( (dow>5)?(dow-5):0 ) ))"


                    The math is: if dow (the day of the week) is bigger than 5, subtract (dow-5) from the day, else, leave day unchanged.



                    Use as:



                    $ ./script 2 2015
                    Last working day of the month = 27





                    share|improve this answer

























                      1














                      With date it is only a two line bash calculation:



                      #!/bin/bash

                      month="$1"
                      year="$2"

                      read -r dow day < <(date -d "$year/$month/1 +1 month -1 day" "+%u %d")

                      echo "Last working day of the month = $(( day - ( (dow>5)?(dow-5):0 ) ))"


                      The math is: if dow (the day of the week) is bigger than 5, subtract (dow-5) from the day, else, leave day unchanged.



                      Use as:



                      $ ./script 2 2015
                      Last working day of the month = 27





                      share|improve this answer























                        1












                        1








                        1






                        With date it is only a two line bash calculation:



                        #!/bin/bash

                        month="$1"
                        year="$2"

                        read -r dow day < <(date -d "$year/$month/1 +1 month -1 day" "+%u %d")

                        echo "Last working day of the month = $(( day - ( (dow>5)?(dow-5):0 ) ))"


                        The math is: if dow (the day of the week) is bigger than 5, subtract (dow-5) from the day, else, leave day unchanged.



                        Use as:



                        $ ./script 2 2015
                        Last working day of the month = 27





                        share|improve this answer












                        With date it is only a two line bash calculation:



                        #!/bin/bash

                        month="$1"
                        year="$2"

                        read -r dow day < <(date -d "$year/$month/1 +1 month -1 day" "+%u %d")

                        echo "Last working day of the month = $(( day - ( (dow>5)?(dow-5):0 ) ))"


                        The math is: if dow (the day of the week) is bigger than 5, subtract (dow-5) from the day, else, leave day unchanged.



                        Use as:



                        $ ./script 2 2015
                        Last working day of the month = 27






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 28 '15 at 2:09







                        user79743




























                            1














                            With ksh93:



                            $ printf "%(%A %B %d)Tn" "October 2018 last working day"
                            Wednesday October 31





                            share|improve this answer

























                              1














                              With ksh93:



                              $ printf "%(%A %B %d)Tn" "October 2018 last working day"
                              Wednesday October 31





                              share|improve this answer























                                1












                                1








                                1






                                With ksh93:



                                $ printf "%(%A %B %d)Tn" "October 2018 last working day"
                                Wednesday October 31





                                share|improve this answer












                                With ksh93:



                                $ printf "%(%A %B %d)Tn" "October 2018 last working day"
                                Wednesday October 31






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 20 at 8:26









                                Stéphane Chazelas

                                299k54563913




                                299k54563913





















                                    0














                                    Your script relies on the screen output of cal. To do things in a more proper way you could use the date command:



                                    #!/bin/bash
                                    # Calculates the last working day (Mon-Fri) of the month

                                    echo "Enter Month and Year:"
                                    read month year
                                    lastdom=`date -d "$year/$month/1 + 1 month - 1 day" "+%d"`
                                    dow=`date -d "$year/$month/1 + 1 month - 1 day" "+%u"`

                                    if [ $dow -ge 6 ]
                                    then
                                    lastwdom=$(($lastdom - $dow + 5))
                                    else
                                    lastwdom=$(($lastdom))
                                    fi

                                    echo "Last working day is $lastwdom"





                                    share|improve this answer

























                                      0














                                      Your script relies on the screen output of cal. To do things in a more proper way you could use the date command:



                                      #!/bin/bash
                                      # Calculates the last working day (Mon-Fri) of the month

                                      echo "Enter Month and Year:"
                                      read month year
                                      lastdom=`date -d "$year/$month/1 + 1 month - 1 day" "+%d"`
                                      dow=`date -d "$year/$month/1 + 1 month - 1 day" "+%u"`

                                      if [ $dow -ge 6 ]
                                      then
                                      lastwdom=$(($lastdom - $dow + 5))
                                      else
                                      lastwdom=$(($lastdom))
                                      fi

                                      echo "Last working day is $lastwdom"





                                      share|improve this answer























                                        0












                                        0








                                        0






                                        Your script relies on the screen output of cal. To do things in a more proper way you could use the date command:



                                        #!/bin/bash
                                        # Calculates the last working day (Mon-Fri) of the month

                                        echo "Enter Month and Year:"
                                        read month year
                                        lastdom=`date -d "$year/$month/1 + 1 month - 1 day" "+%d"`
                                        dow=`date -d "$year/$month/1 + 1 month - 1 day" "+%u"`

                                        if [ $dow -ge 6 ]
                                        then
                                        lastwdom=$(($lastdom - $dow + 5))
                                        else
                                        lastwdom=$(($lastdom))
                                        fi

                                        echo "Last working day is $lastwdom"





                                        share|improve this answer












                                        Your script relies on the screen output of cal. To do things in a more proper way you could use the date command:



                                        #!/bin/bash
                                        # Calculates the last working day (Mon-Fri) of the month

                                        echo "Enter Month and Year:"
                                        read month year
                                        lastdom=`date -d "$year/$month/1 + 1 month - 1 day" "+%d"`
                                        dow=`date -d "$year/$month/1 + 1 month - 1 day" "+%u"`

                                        if [ $dow -ge 6 ]
                                        then
                                        lastwdom=$(($lastdom - $dow + 5))
                                        else
                                        lastwdom=$(($lastdom))
                                        fi

                                        echo "Last working day is $lastwdom"






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 24 '15 at 9:44









                                        dr01

                                        15.9k114970




                                        15.9k114970





















                                            0














                                            Your script repeats the print because awk is receiving two lines from egrep. But that has already been covered in other answers.



                                            I want to explain some alternative way to solve the problem, shorter, easier.



                                            The program cal could print the week starting on monday (which simplifies the math) when called as this cal -NMC month year. Using that:



                                            #!/bin/bash

                                            lastday()
                                            printf 'Last Working Date of %s/%s = ' "$1" "$2";
                                            cal -NMC "$1" "$2"

                                            mon="$1"
                                            year="$2"
                                            lastday "$mon" "$year"


                                            Description:



                                            /[0-9]+/ Select lines with numbers (avoid empty line).



                                            NF>5?5:NF Math: If more fields than 5 result is 5, else NF.



                                            val=$( ... ) Select the value of the field.



                                            END print val ' Only print the value of the last line (line with numbers).



                                            Call it like this:



                                            $ ./test.sh 4 2015
                                            Last Working Date of 4/2015 = 30
                                            $ ./test.sh 5 2015
                                            Last Working Date of 5/2015 = 29
                                            $ ./test.sh 7 2015
                                            Last Working Date of 7/2015 = 31
                                            $ ./test.sh 1 2015
                                            Last Working Date of 1/2015 = 30
                                            $ ./test.sh 9 2015
                                            Last Working Date of 9/2015 = 30
                                            $





                                            share|improve this answer



























                                              0














                                              Your script repeats the print because awk is receiving two lines from egrep. But that has already been covered in other answers.



                                              I want to explain some alternative way to solve the problem, shorter, easier.



                                              The program cal could print the week starting on monday (which simplifies the math) when called as this cal -NMC month year. Using that:



                                              #!/bin/bash

                                              lastday()
                                              printf 'Last Working Date of %s/%s = ' "$1" "$2";
                                              cal -NMC "$1" "$2"

                                              mon="$1"
                                              year="$2"
                                              lastday "$mon" "$year"


                                              Description:



                                              /[0-9]+/ Select lines with numbers (avoid empty line).



                                              NF>5?5:NF Math: If more fields than 5 result is 5, else NF.



                                              val=$( ... ) Select the value of the field.



                                              END print val ' Only print the value of the last line (line with numbers).



                                              Call it like this:



                                              $ ./test.sh 4 2015
                                              Last Working Date of 4/2015 = 30
                                              $ ./test.sh 5 2015
                                              Last Working Date of 5/2015 = 29
                                              $ ./test.sh 7 2015
                                              Last Working Date of 7/2015 = 31
                                              $ ./test.sh 1 2015
                                              Last Working Date of 1/2015 = 30
                                              $ ./test.sh 9 2015
                                              Last Working Date of 9/2015 = 30
                                              $





                                              share|improve this answer

























                                                0












                                                0








                                                0






                                                Your script repeats the print because awk is receiving two lines from egrep. But that has already been covered in other answers.



                                                I want to explain some alternative way to solve the problem, shorter, easier.



                                                The program cal could print the week starting on monday (which simplifies the math) when called as this cal -NMC month year. Using that:



                                                #!/bin/bash

                                                lastday()
                                                printf 'Last Working Date of %s/%s = ' "$1" "$2";
                                                cal -NMC "$1" "$2"

                                                mon="$1"
                                                year="$2"
                                                lastday "$mon" "$year"


                                                Description:



                                                /[0-9]+/ Select lines with numbers (avoid empty line).



                                                NF>5?5:NF Math: If more fields than 5 result is 5, else NF.



                                                val=$( ... ) Select the value of the field.



                                                END print val ' Only print the value of the last line (line with numbers).



                                                Call it like this:



                                                $ ./test.sh 4 2015
                                                Last Working Date of 4/2015 = 30
                                                $ ./test.sh 5 2015
                                                Last Working Date of 5/2015 = 29
                                                $ ./test.sh 7 2015
                                                Last Working Date of 7/2015 = 31
                                                $ ./test.sh 1 2015
                                                Last Working Date of 1/2015 = 30
                                                $ ./test.sh 9 2015
                                                Last Working Date of 9/2015 = 30
                                                $





                                                share|improve this answer














                                                Your script repeats the print because awk is receiving two lines from egrep. But that has already been covered in other answers.



                                                I want to explain some alternative way to solve the problem, shorter, easier.



                                                The program cal could print the week starting on monday (which simplifies the math) when called as this cal -NMC month year. Using that:



                                                #!/bin/bash

                                                lastday()
                                                printf 'Last Working Date of %s/%s = ' "$1" "$2";
                                                cal -NMC "$1" "$2"

                                                mon="$1"
                                                year="$2"
                                                lastday "$mon" "$year"


                                                Description:



                                                /[0-9]+/ Select lines with numbers (avoid empty line).



                                                NF>5?5:NF Math: If more fields than 5 result is 5, else NF.



                                                val=$( ... ) Select the value of the field.



                                                END print val ' Only print the value of the last line (line with numbers).



                                                Call it like this:



                                                $ ./test.sh 4 2015
                                                Last Working Date of 4/2015 = 30
                                                $ ./test.sh 5 2015
                                                Last Working Date of 5/2015 = 29
                                                $ ./test.sh 7 2015
                                                Last Working Date of 7/2015 = 31
                                                $ ./test.sh 1 2015
                                                Last Working Date of 1/2015 = 30
                                                $ ./test.sh 9 2015
                                                Last Working Date of 9/2015 = 30
                                                $






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Aug 25 '15 at 2:14

























                                                answered Aug 25 '15 at 1:09







                                                user79743




























                                                    0














                                                    read -p "Please enter date 'yyyymmdd':" dat
                                                    yy=`echo $dat|cut -c 1-4`
                                                    mm=`echo $dat|cut -c 5-6`

                                                    case $mm in

                                                    01|02|03)
                                                    mm=12
                                                    yy=`expr $yy - 1`
                                                    dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                    echo $yy$mm$dd
                                                    ;;

                                                    04|05|06)
                                                    mm=03
                                                    dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                    echo $yy$mm$dd
                                                    ;;

                                                    07|08|09)
                                                    mm=06
                                                    dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                    echo $yy$mm$dd
                                                    ;;

                                                    10|11|12)
                                                    mm=09
                                                    dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                    echo $yy$mm$dd
                                                    ;;

                                                    *) echo "Invalid month"
                                                    exit;;
                                                    esac





                                                    share|improve this answer



























                                                      0














                                                      read -p "Please enter date 'yyyymmdd':" dat
                                                      yy=`echo $dat|cut -c 1-4`
                                                      mm=`echo $dat|cut -c 5-6`

                                                      case $mm in

                                                      01|02|03)
                                                      mm=12
                                                      yy=`expr $yy - 1`
                                                      dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                      echo $yy$mm$dd
                                                      ;;

                                                      04|05|06)
                                                      mm=03
                                                      dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                      echo $yy$mm$dd
                                                      ;;

                                                      07|08|09)
                                                      mm=06
                                                      dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                      echo $yy$mm$dd
                                                      ;;

                                                      10|11|12)
                                                      mm=09
                                                      dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                      echo $yy$mm$dd
                                                      ;;

                                                      *) echo "Invalid month"
                                                      exit;;
                                                      esac





                                                      share|improve this answer

























                                                        0












                                                        0








                                                        0






                                                        read -p "Please enter date 'yyyymmdd':" dat
                                                        yy=`echo $dat|cut -c 1-4`
                                                        mm=`echo $dat|cut -c 5-6`

                                                        case $mm in

                                                        01|02|03)
                                                        mm=12
                                                        yy=`expr $yy - 1`
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        04|05|06)
                                                        mm=03
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        07|08|09)
                                                        mm=06
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        10|11|12)
                                                        mm=09
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        *) echo "Invalid month"
                                                        exit;;
                                                        esac





                                                        share|improve this answer














                                                        read -p "Please enter date 'yyyymmdd':" dat
                                                        yy=`echo $dat|cut -c 1-4`
                                                        mm=`echo $dat|cut -c 5-6`

                                                        case $mm in

                                                        01|02|03)
                                                        mm=12
                                                        yy=`expr $yy - 1`
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        04|05|06)
                                                        mm=03
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        07|08|09)
                                                        mm=06
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        10|11|12)
                                                        mm=09
                                                        dd=`cal $mm $yy|awk -F " " 'print $6'|grep -v ^$|tail -1`
                                                        echo $yy$mm$dd
                                                        ;;

                                                        *) echo "Invalid month"
                                                        exit;;
                                                        esac






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Jun 20 at 9:36









                                                        perror

                                                        1,90441835




                                                        1,90441835










                                                        answered Jun 20 at 8:13









                                                        Sushmit

                                                        1




                                                        1



























                                                            draft saved

                                                            draft discarded
















































                                                            Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f224808%2flast-working-day-of-the-month%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown






                                                            Popular posts from this blog

                                                            How to check contact read email or not when send email to Individual?

                                                            Displaying single band from multi-band raster using QGIS

                                                            How many registers does an x86_64 CPU actually have?