LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2021, 02:11 AM   #1
rvsr
LQ Newbie
 
Registered: Apr 2021
Posts: 9

Rep: Reputation: Disabled
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

Last edited by rvsr; 04-23-2021 at 08:25 AM.
 
Old 04-22-2021, 02:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,612
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
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?
 
Old 04-22-2021, 02:25 AM   #3
rvsr
LQ Newbie
 
Registered: Apr 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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?
 
Old 04-22-2021, 09:30 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,810

Rep: Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974
Quote:
Originally Posted by rvsr View Post
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.
 
1 members found this post helpful.
Old 04-22-2021, 11:19 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,460

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
Quote:
Originally Posted by rvsr View Post
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"?
 
Old 04-23-2021, 05:25 AM   #6
rvsr
LQ Newbie
 
Registered: Apr 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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
 
Old 04-23-2021, 05:28 AM   #7
rvsr
LQ Newbie
 
Registered: Apr 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rnturn View Post
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

Last edited by rvsr; 04-23-2021 at 08:26 AM.
 
Old 04-23-2021, 05:41 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,185

Rep: Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377
Quote:
Originally Posted by rvsr View Post
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 View Post
Can you suggest how to get rid of this
Decide what your script is supposed to be doing and write it that way.
 
Old 04-24-2021, 03:30 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,741

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash script to run monthly and yearly 1s440 Programming 15 04-20-2021 10:18 AM
BASH script for Before and after Health checks of multiple systems for monthly maintenance SeaPhor Programming 7 03-05-2017 09:28 AM
cron.monthly how do they run? charu Linux - Newbie 1 12-08-2010 04:55 PM
pppd, wvdial, ip-down script | monthly bandwidth measure number-g Linux - Networking 1 05-02-2009 06:08 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration