Bash script to test if it's the first Monday of the month












1















I have a bash script that takes a handful of files and sets them up for FTP to a site that processes the one of the setup files. We are looking to find away to have the other file go up on the first Monday of the month but I am not sure how to put that in the bash script. I have seen stuff around using crontab but the first part and the last part of the script would be exactly the same and could cause issues if we had 2 different scripts.



only putting in a part of the script that I'm looking at making the change to.



#!/bin/bash
...

e_file="/tmp/tmpemail.$(date +%s).txt"
file1='/usr/local/filename1'
file2='/usr/local/filename2'
relayserver='relay-server.example.com'

#ftp info
FTP_USER='ftpuser' #not the actual FTP User Name
FTP_DEST_PATH='/'

...

echo -e "Starting Tunnel and SFTP Process"
# make ssh tunnel for access to SFTP Site
ssh -L 9022:ftp.example.com:22 serviceaccount@$relay_server -Nf >/dev/null 2&>1
proc=`ps -ef | grep "ssh -L 9022:ftp.example.com:22" | awk '{print $2}'`

#checks to see if the tunnel opened correctly then proceeds to push to FTP Site
if [ "${proc}" != "" ]; then

#looking for first monday, was thinking of first day but the crontab only runs on monday to friday
ifStart=`date '+%d'`
if [ $ifStart == 01 ]; then

echo -e "File 1 & File2 sent to FTP Site" >> $e_file
$SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
cd $FTP_DEST_PATH
put $file1
put $file2
bye
END

else

echo -e "file 2 sent to FTP" >> $e_file
$SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
cd $FTP_DEST_PATH
put $file2
bye
END

fi

echo "killing ssh tunnel - $proc"
kill $proc

else

...


I am looking to be pointed in the right direction of getting the if statement for the first Monday of the month where I have to comment located. Any ideas to get around this?



Added Note:
This Script has to run every weekday of the month to upload the files to be processed.










share|improve this question





























    1















    I have a bash script that takes a handful of files and sets them up for FTP to a site that processes the one of the setup files. We are looking to find away to have the other file go up on the first Monday of the month but I am not sure how to put that in the bash script. I have seen stuff around using crontab but the first part and the last part of the script would be exactly the same and could cause issues if we had 2 different scripts.



    only putting in a part of the script that I'm looking at making the change to.



    #!/bin/bash
    ...

    e_file="/tmp/tmpemail.$(date +%s).txt"
    file1='/usr/local/filename1'
    file2='/usr/local/filename2'
    relayserver='relay-server.example.com'

    #ftp info
    FTP_USER='ftpuser' #not the actual FTP User Name
    FTP_DEST_PATH='/'

    ...

    echo -e "Starting Tunnel and SFTP Process"
    # make ssh tunnel for access to SFTP Site
    ssh -L 9022:ftp.example.com:22 serviceaccount@$relay_server -Nf >/dev/null 2&>1
    proc=`ps -ef | grep "ssh -L 9022:ftp.example.com:22" | awk '{print $2}'`

    #checks to see if the tunnel opened correctly then proceeds to push to FTP Site
    if [ "${proc}" != "" ]; then

    #looking for first monday, was thinking of first day but the crontab only runs on monday to friday
    ifStart=`date '+%d'`
    if [ $ifStart == 01 ]; then

    echo -e "File 1 & File2 sent to FTP Site" >> $e_file
    $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
    cd $FTP_DEST_PATH
    put $file1
    put $file2
    bye
    END

    else

    echo -e "file 2 sent to FTP" >> $e_file
    $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
    cd $FTP_DEST_PATH
    put $file2
    bye
    END

    fi

    echo "killing ssh tunnel - $proc"
    kill $proc

    else

    ...


    I am looking to be pointed in the right direction of getting the if statement for the first Monday of the month where I have to comment located. Any ideas to get around this?



    Added Note:
    This Script has to run every weekday of the month to upload the files to be processed.










    share|improve this question



























      1












      1








      1


      1






      I have a bash script that takes a handful of files and sets them up for FTP to a site that processes the one of the setup files. We are looking to find away to have the other file go up on the first Monday of the month but I am not sure how to put that in the bash script. I have seen stuff around using crontab but the first part and the last part of the script would be exactly the same and could cause issues if we had 2 different scripts.



      only putting in a part of the script that I'm looking at making the change to.



      #!/bin/bash
      ...

      e_file="/tmp/tmpemail.$(date +%s).txt"
      file1='/usr/local/filename1'
      file2='/usr/local/filename2'
      relayserver='relay-server.example.com'

      #ftp info
      FTP_USER='ftpuser' #not the actual FTP User Name
      FTP_DEST_PATH='/'

      ...

      echo -e "Starting Tunnel and SFTP Process"
      # make ssh tunnel for access to SFTP Site
      ssh -L 9022:ftp.example.com:22 serviceaccount@$relay_server -Nf >/dev/null 2&>1
      proc=`ps -ef | grep "ssh -L 9022:ftp.example.com:22" | awk '{print $2}'`

      #checks to see if the tunnel opened correctly then proceeds to push to FTP Site
      if [ "${proc}" != "" ]; then

      #looking for first monday, was thinking of first day but the crontab only runs on monday to friday
      ifStart=`date '+%d'`
      if [ $ifStart == 01 ]; then

      echo -e "File 1 & File2 sent to FTP Site" >> $e_file
      $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
      cd $FTP_DEST_PATH
      put $file1
      put $file2
      bye
      END

      else

      echo -e "file 2 sent to FTP" >> $e_file
      $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
      cd $FTP_DEST_PATH
      put $file2
      bye
      END

      fi

      echo "killing ssh tunnel - $proc"
      kill $proc

      else

      ...


      I am looking to be pointed in the right direction of getting the if statement for the first Monday of the month where I have to comment located. Any ideas to get around this?



      Added Note:
      This Script has to run every weekday of the month to upload the files to be processed.










      share|improve this question
















      I have a bash script that takes a handful of files and sets them up for FTP to a site that processes the one of the setup files. We are looking to find away to have the other file go up on the first Monday of the month but I am not sure how to put that in the bash script. I have seen stuff around using crontab but the first part and the last part of the script would be exactly the same and could cause issues if we had 2 different scripts.



      only putting in a part of the script that I'm looking at making the change to.



      #!/bin/bash
      ...

      e_file="/tmp/tmpemail.$(date +%s).txt"
      file1='/usr/local/filename1'
      file2='/usr/local/filename2'
      relayserver='relay-server.example.com'

      #ftp info
      FTP_USER='ftpuser' #not the actual FTP User Name
      FTP_DEST_PATH='/'

      ...

      echo -e "Starting Tunnel and SFTP Process"
      # make ssh tunnel for access to SFTP Site
      ssh -L 9022:ftp.example.com:22 serviceaccount@$relay_server -Nf >/dev/null 2&>1
      proc=`ps -ef | grep "ssh -L 9022:ftp.example.com:22" | awk '{print $2}'`

      #checks to see if the tunnel opened correctly then proceeds to push to FTP Site
      if [ "${proc}" != "" ]; then

      #looking for first monday, was thinking of first day but the crontab only runs on monday to friday
      ifStart=`date '+%d'`
      if [ $ifStart == 01 ]; then

      echo -e "File 1 & File2 sent to FTP Site" >> $e_file
      $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
      cd $FTP_DEST_PATH
      put $file1
      put $file2
      bye
      END

      else

      echo -e "file 2 sent to FTP" >> $e_file
      $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
      cd $FTP_DEST_PATH
      put $file2
      bye
      END

      fi

      echo "killing ssh tunnel - $proc"
      kill $proc

      else

      ...


      I am looking to be pointed in the right direction of getting the if statement for the first Monday of the month where I have to comment located. Any ideas to get around this?



      Added Note:
      This Script has to run every weekday of the month to upload the files to be processed.







      linux shell-script date






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 24 '18 at 23:56









      Jeff Schaller

      39.5k1054126




      39.5k1054126










      asked May 24 '18 at 20:38









      thebtmthebtm

      761515




      761515






















          3 Answers
          3






          active

          oldest

          votes


















          9














          I do not have time to read all the script but here is the idea:
          with date command get the name of the day in week:



          we=$(LC_TIME=C date +%A)


          (LC_TIME=C is used to get English name of the day of week)



          and then get day in the month



          dm=$(date +%d)


          and then check if the day is less than 8 and day of week is Monday:



          if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
          then
          .....
          fi





          share|improve this answer





















          • 1





            it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

            – Jeff Schaller
            May 25 '18 at 0:40











          • @JeffSchaller, thank you, will edit my answer

            – Romeo Ninov
            May 25 '18 at 2:09






          • 2





            You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

            – glenn jackman
            May 25 '18 at 2:12








          • 1





            @thebtm, no. This is the way to set variable only for execution of current command.

            – Romeo Ninov
            May 28 '18 at 17:18






          • 1





            I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

            – Jeff Schaller
            May 29 '18 at 15:55



















          3














          Put the script in question into your crontab:



          0 1 * * 1 [[ "$(/bin/date +%d)" -le 7 ]] && /path/to/script.sh


          On every Monday at 0100, it will check to see if the date is less than or equal to the seventh, and if so, run the script.



          Bonus to using this is it's very easy to reschedule for Tuesdays without having to edit the script.






          share|improve this answer





















          • 1





            You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

            – glenn jackman
            May 25 '18 at 2:13











          • Done and done; good catch.

            – DopeGhoti
            May 25 '18 at 15:32



















          2














          You could have cron run a script on every Monday and have the script check whether the day of the month is 1 to 7.



          This check can either be integrated in your main script or you can write a wrapper script so that you do not have to make this change to the main script so that it can run.



          if [[ $(date +%d) =~ 0[1-7] ]]; then
          : run script
          fi


          Checking both day of week and day of month:



          if [[ $(date +%w%d) =~ 10[1-7] ]]; then
          : run script
          fi





          share|improve this answer


























          • double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

            – thebtm
            May 24 '18 at 20:48








          • 1





            @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

            – Hauke Laging
            May 24 '18 at 20:53











          • This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

            – thebtm
            May 24 '18 at 21:10











          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%2f445854%2fbash-script-to-test-if-its-the-first-monday-of-the-month%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          I do not have time to read all the script but here is the idea:
          with date command get the name of the day in week:



          we=$(LC_TIME=C date +%A)


          (LC_TIME=C is used to get English name of the day of week)



          and then get day in the month



          dm=$(date +%d)


          and then check if the day is less than 8 and day of week is Monday:



          if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
          then
          .....
          fi





          share|improve this answer





















          • 1





            it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

            – Jeff Schaller
            May 25 '18 at 0:40











          • @JeffSchaller, thank you, will edit my answer

            – Romeo Ninov
            May 25 '18 at 2:09






          • 2





            You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

            – glenn jackman
            May 25 '18 at 2:12








          • 1





            @thebtm, no. This is the way to set variable only for execution of current command.

            – Romeo Ninov
            May 28 '18 at 17:18






          • 1





            I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

            – Jeff Schaller
            May 29 '18 at 15:55
















          9














          I do not have time to read all the script but here is the idea:
          with date command get the name of the day in week:



          we=$(LC_TIME=C date +%A)


          (LC_TIME=C is used to get English name of the day of week)



          and then get day in the month



          dm=$(date +%d)


          and then check if the day is less than 8 and day of week is Monday:



          if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
          then
          .....
          fi





          share|improve this answer





















          • 1





            it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

            – Jeff Schaller
            May 25 '18 at 0:40











          • @JeffSchaller, thank you, will edit my answer

            – Romeo Ninov
            May 25 '18 at 2:09






          • 2





            You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

            – glenn jackman
            May 25 '18 at 2:12








          • 1





            @thebtm, no. This is the way to set variable only for execution of current command.

            – Romeo Ninov
            May 28 '18 at 17:18






          • 1





            I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

            – Jeff Schaller
            May 29 '18 at 15:55














          9












          9








          9







          I do not have time to read all the script but here is the idea:
          with date command get the name of the day in week:



          we=$(LC_TIME=C date +%A)


          (LC_TIME=C is used to get English name of the day of week)



          and then get day in the month



          dm=$(date +%d)


          and then check if the day is less than 8 and day of week is Monday:



          if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
          then
          .....
          fi





          share|improve this answer















          I do not have time to read all the script but here is the idea:
          with date command get the name of the day in week:



          we=$(LC_TIME=C date +%A)


          (LC_TIME=C is used to get English name of the day of week)



          and then get day in the month



          dm=$(date +%d)


          and then check if the day is less than 8 and day of week is Monday:



          if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
          then
          .....
          fi






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 29 '18 at 18:00

























          answered May 24 '18 at 20:49









          Romeo NinovRomeo Ninov

          5,71831928




          5,71831928








          • 1





            it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

            – Jeff Schaller
            May 25 '18 at 0:40











          • @JeffSchaller, thank you, will edit my answer

            – Romeo Ninov
            May 25 '18 at 2:09






          • 2





            You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

            – glenn jackman
            May 25 '18 at 2:12








          • 1





            @thebtm, no. This is the way to set variable only for execution of current command.

            – Romeo Ninov
            May 28 '18 at 17:18






          • 1





            I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

            – Jeff Schaller
            May 29 '18 at 15:55














          • 1





            it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

            – Jeff Schaller
            May 25 '18 at 0:40











          • @JeffSchaller, thank you, will edit my answer

            – Romeo Ninov
            May 25 '18 at 2:09






          • 2





            You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

            – glenn jackman
            May 25 '18 at 2:12








          • 1





            @thebtm, no. This is the way to set variable only for execution of current command.

            – Romeo Ninov
            May 28 '18 at 17:18






          • 1





            I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

            – Jeff Schaller
            May 29 '18 at 15:55








          1




          1





          it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

          – Jeff Schaller
          May 25 '18 at 0:40





          it might be worth adding an LC_TIME=C environment variable/prefix before the call to date +%A, to ensure you get English names (or perhaps setting LC_TIME to an English language).

          – Jeff Schaller
          May 25 '18 at 0:40













          @JeffSchaller, thank you, will edit my answer

          – Romeo Ninov
          May 25 '18 at 2:09





          @JeffSchaller, thank you, will edit my answer

          – Romeo Ninov
          May 25 '18 at 2:09




          2




          2





          You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

          – glenn jackman
          May 25 '18 at 2:12







          You only need to call date once using a process substitution: read we dm < <(date "+%A %d")

          – glenn jackman
          May 25 '18 at 2:12






          1




          1





          @thebtm, no. This is the way to set variable only for execution of current command.

          – Romeo Ninov
          May 28 '18 at 17:18





          @thebtm, no. This is the way to set variable only for execution of current command.

          – Romeo Ninov
          May 28 '18 at 17:18




          1




          1





          I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

          – Jeff Schaller
          May 29 '18 at 15:55





          I believe @thebtm has a good point; if LC_TIME is not already exported, then it will not be set for the call to date. Using a semicolon wouldn't help, either. Either export LC_TIME or set it inside the command substitution: we=$(LC_TIME=C date +%A)

          – Jeff Schaller
          May 29 '18 at 15:55













          3














          Put the script in question into your crontab:



          0 1 * * 1 [[ "$(/bin/date +%d)" -le 7 ]] && /path/to/script.sh


          On every Monday at 0100, it will check to see if the date is less than or equal to the seventh, and if so, run the script.



          Bonus to using this is it's very easy to reschedule for Tuesdays without having to edit the script.






          share|improve this answer





















          • 1





            You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

            – glenn jackman
            May 25 '18 at 2:13











          • Done and done; good catch.

            – DopeGhoti
            May 25 '18 at 15:32
















          3














          Put the script in question into your crontab:



          0 1 * * 1 [[ "$(/bin/date +%d)" -le 7 ]] && /path/to/script.sh


          On every Monday at 0100, it will check to see if the date is less than or equal to the seventh, and if so, run the script.



          Bonus to using this is it's very easy to reschedule for Tuesdays without having to edit the script.






          share|improve this answer





















          • 1





            You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

            – glenn jackman
            May 25 '18 at 2:13











          • Done and done; good catch.

            – DopeGhoti
            May 25 '18 at 15:32














          3












          3








          3







          Put the script in question into your crontab:



          0 1 * * 1 [[ "$(/bin/date +%d)" -le 7 ]] && /path/to/script.sh


          On every Monday at 0100, it will check to see if the date is less than or equal to the seventh, and if so, run the script.



          Bonus to using this is it's very easy to reschedule for Tuesdays without having to edit the script.






          share|improve this answer















          Put the script in question into your crontab:



          0 1 * * 1 [[ "$(/bin/date +%d)" -le 7 ]] && /path/to/script.sh


          On every Monday at 0100, it will check to see if the date is less than or equal to the seventh, and if so, run the script.



          Bonus to using this is it's very easy to reschedule for Tuesdays without having to edit the script.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 14 at 15:11

























          answered May 24 '18 at 21:06









          DopeGhotiDopeGhoti

          44.2k55683




          44.2k55683








          • 1





            You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

            – glenn jackman
            May 25 '18 at 2:13











          • Done and done; good catch.

            – DopeGhoti
            May 25 '18 at 15:32














          • 1





            You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

            – glenn jackman
            May 25 '18 at 2:13











          • Done and done; good catch.

            – DopeGhoti
            May 25 '18 at 15:32








          1




          1





          You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

          – glenn jackman
          May 25 '18 at 2:13





          You need to escape % in the crontab: date "+%d" -- documented in crontab(5)

          – glenn jackman
          May 25 '18 at 2:13













          Done and done; good catch.

          – DopeGhoti
          May 25 '18 at 15:32





          Done and done; good catch.

          – DopeGhoti
          May 25 '18 at 15:32











          2














          You could have cron run a script on every Monday and have the script check whether the day of the month is 1 to 7.



          This check can either be integrated in your main script or you can write a wrapper script so that you do not have to make this change to the main script so that it can run.



          if [[ $(date +%d) =~ 0[1-7] ]]; then
          : run script
          fi


          Checking both day of week and day of month:



          if [[ $(date +%w%d) =~ 10[1-7] ]]; then
          : run script
          fi





          share|improve this answer


























          • double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

            – thebtm
            May 24 '18 at 20:48








          • 1





            @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

            – Hauke Laging
            May 24 '18 at 20:53











          • This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

            – thebtm
            May 24 '18 at 21:10
















          2














          You could have cron run a script on every Monday and have the script check whether the day of the month is 1 to 7.



          This check can either be integrated in your main script or you can write a wrapper script so that you do not have to make this change to the main script so that it can run.



          if [[ $(date +%d) =~ 0[1-7] ]]; then
          : run script
          fi


          Checking both day of week and day of month:



          if [[ $(date +%w%d) =~ 10[1-7] ]]; then
          : run script
          fi





          share|improve this answer


























          • double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

            – thebtm
            May 24 '18 at 20:48








          • 1





            @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

            – Hauke Laging
            May 24 '18 at 20:53











          • This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

            – thebtm
            May 24 '18 at 21:10














          2












          2








          2







          You could have cron run a script on every Monday and have the script check whether the day of the month is 1 to 7.



          This check can either be integrated in your main script or you can write a wrapper script so that you do not have to make this change to the main script so that it can run.



          if [[ $(date +%d) =~ 0[1-7] ]]; then
          : run script
          fi


          Checking both day of week and day of month:



          if [[ $(date +%w%d) =~ 10[1-7] ]]; then
          : run script
          fi





          share|improve this answer















          You could have cron run a script on every Monday and have the script check whether the day of the month is 1 to 7.



          This check can either be integrated in your main script or you can write a wrapper script so that you do not have to make this change to the main script so that it can run.



          if [[ $(date +%d) =~ 0[1-7] ]]; then
          : run script
          fi


          Checking both day of week and day of month:



          if [[ $(date +%w%d) =~ 10[1-7] ]]; then
          : run script
          fi






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 24 '18 at 20:52

























          answered May 24 '18 at 20:45









          Hauke LagingHauke Laging

          56.1k1285135




          56.1k1285135













          • double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

            – thebtm
            May 24 '18 at 20:48








          • 1





            @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

            – Hauke Laging
            May 24 '18 at 20:53











          • This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

            – thebtm
            May 24 '18 at 21:10



















          • double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

            – thebtm
            May 24 '18 at 20:48








          • 1





            @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

            – Hauke Laging
            May 24 '18 at 20:53











          • This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

            – thebtm
            May 24 '18 at 21:10

















          double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

          – thebtm
          May 24 '18 at 20:48







          double condition where it checks for if its the day is within the first week and if the day is a monday if [[ $(date +%d) =~ 0[1-7] -a $(date +%u) == 1 ]]?

          – thebtm
          May 24 '18 at 20:48






          1




          1





          @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

          – Hauke Laging
          May 24 '18 at 20:53





          @thebtm No, -a is for [ ], not for [[ ]]. No need to call date twice. See my edit.

          – Hauke Laging
          May 24 '18 at 20:53













          This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

          – thebtm
          May 24 '18 at 21:10





          This Answer works too but the other one is easier to read for if/when others need to take over the script. Thank You for the info though.

          – thebtm
          May 24 '18 at 21:10


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f445854%2fbash-script-to-test-if-its-the-first-monday-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 reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?