LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to run monthly (https://www.linuxquestions.org/questions/programming-9/script-to-run-monthly-4175694075/)

rvsr 04-22-2021 02:11 AM

script to run monthly
 
Hi all,

Firstly I am new to bash script.
I want to run this script every month (which is inside the forloop) and if should execute only execute when the $b matches $a.I want this count to be executed in both cases
I am not sure if this is the right way to do it. can you please suggest.

Code:

for VARIABLE in file1
do
    command1 on $VARIABLE
    command2
    if [ $b == $a ]
    then
    command 1
    command 2
    count = count +1
    fi;
done


Turbocapitalist 04-22-2021 02:19 AM

Welcome.

You should look at cron instead. That is the normal way of scheduling a script to run on a recurring schedule. See 'man 5 crontab' and 'man 1 crontab' for the authoritative reference manuals. The reference manual pages are not tutorials but important to learn to navigate, so cross check them as you find tutorials online.

Which distro is this for, including version? Also, if this is homework, then which additional information has been provided about the direction you should take in looking for a solution?

rvsr 04-22-2021 02:25 AM

Quote:

Originally Posted by Turbocapitalist (Post 6243606)
Welcome.

You should look at cron instead. That is the normal way of scheduling a script to run on a recurring schedule. See 'man 5 crontab' and 'man 1 crontab' for the authoritative reference manuals. The reference manual pages are not tutorials but important to learn to navigate, so cross check them as you find tutorials online.

Which distro is this for, including version? Also, if this is homework,
then which additional information has been provided about the direction you should take in looking for a solution?


Hi, thanks for quick reply. I am running this script on ubuntu 14. Its not homework ofcourse. Yes to use the cronjob also, I am little confused how to do it. For example this script should run monthly and to run only on the particular month and date ( if condition should be matched ) not sure if i have to schedule cronjob twice?

TB0ne 04-22-2021 09:30 AM

Quote:

Originally Posted by rvsr (Post 6243602)
Hi all,
Firstly I am new to bash script. I want to run this script every month (which is inside the forloop) and if should execute only execute when the $b matches $a.I want this count to be executed in both cases I am not sure if this is the right way to do it. can you please suggest.
Code:

b=$(date +"%Y%m%d)
a=$(date +"%Y1030)
count = 0
for VARIABLE in file1
do
    command1 on $VARIABLE
    command2
    if [ $b == $a ]
    then
    command 1
    command 2
    count = count +1
    fi;
done


This sounds and looks VERY much like homework. And you're asking if this is the 'right way' to do it...does your solution WORK? Have you actually tried it??

There are always multiple ways to do things, but in this case you're not thinking things through...how is your script going to check if the dates match, if you don't actually RUN the script every day? And if you're manually running the script...what's the point of the date-check? As mentioned, cron can run things whenever you want...as an example:
https://www.linuxquestions.org/quest...2/#post4654486

...modify as you see fit. You then don't need the date-checks at all.

rnturn 04-22-2021 11:19 PM

Quote:

Originally Posted by rvsr (Post 6243602)
Hi all,

Firstly I am new to bash script.
I want to run this script every month (which is inside the forloop) and if should execute only execute when the $b matches $a.I want this count to be executed in both cases
I am not sure if this is the right way to do it. can you please suggest.

Code:

b=$(date +"%Y%m%d)
a=$(date +"%Y1030)
count = 0

<snip>


See: "man cron"

I'd lose the date construction and comparison (and the syntax errors in those lines). It forces this to have to run daily but only perform something useful on a specific month and day. Of course, if, for some reason, cron is not available... you'll have to correct those errors. Then you'll need to figure out how to schedule this script to run daily. "sleep 86400" at the end? Resubmit itself using something like "at -f $0 some-time-of-day tomorrow"?

rvsr 04-23-2021 05:25 AM

Quote:

Originally Posted by TB0ne (Post 6243743)
This sounds and looks VERY much like homework. And you're asking if this is the 'right way' to do it...does your solution WORK? Have you actually tried it??

There are always multiple ways to do things, but in this case you're not thinking things through...how is your script going to check if the dates match, if you don't actually RUN the script every day? And if you're manually running the script...what's the point of the date-check? As mentioned, cron can run things whenever you want...as an example:
https://www.linuxquestions.org/quest...2/#post4654486

...modify as you see fit. You then don't need the date-checks at all.

Its not a homework though. I am building a script

rvsr 04-23-2021 05:28 AM

Quote:

Originally Posted by rnturn (Post 6244029)
See: "man cron"

I'd lose the date construction and comparison (and the syntax errors in those lines). It forces this to have to run daily but only perform something useful on a specific month and day. Of course, if, for some reason, cron is not available... you'll have to correct those errors. Then you'll need to figure out how to schedule this script to run daily. "sleep 86400" at the end? Resubmit itself using something like "at -f $0 some-time-of-day tomorrow"?

Hi, Thanks for the reply. the code seems to work But it seems like it tries to create directory each and everytime.

Code:

mkdir: cannot create directory ‘/dir/daily/’: File exists
Can you suggest how to get rid of this

Code:

for VARIABLE in file1
do
    command1 on $VARIABLE
    if [ $b == $a ]
    then
    command 1
    command 2
    count = count +1
    fi;
done


TenTenths 04-23-2021 05:41 AM

Quote:

Originally Posted by rvsr (Post 6244095)
Hi, Thanks for the reply. the code seems to work But it seems like it tries to create directory each and everytime.

That's because you have mkdir in your for loop so it gets called every time.
Quote:

Originally Posted by rvsr (Post 6244095)
Can you suggest how to get rid of this

Decide what your script is supposed to be doing and write it that way.

MadeInGermany 04-24-2021 03:30 AM

If the mkdir target is dynamic e.g.
Code:

mkdir "$variable"
then it can make sense to have it in the loop.
Then you can avoid the error with a "dir exist" test
Code:

[ -d "$variable" ] || mkdir "$variable"
The || continues with the following command only if the previous command had bad exit status (false, a non-zero exit code).
Or use the -p option
Code:

mkdir -p "$variable"
You find the options described in the man page:
Code:

man mkdir
You can even Google for man mkdir linux


All times are GMT -5. The time now is 01:32 PM.