Script should check and run only on Sunday


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script should check and run only on Sunday
# 36  
Old 07-16-2014
Thanks you so much bakunin...
I will keep this in mind for my future automations

---------- Post updated 07-16-14 at 06:19 AM ---------- Previous update was 07-15-14 at 06:59 AM ----------

Hi bakunin...
I am not able to print the attempt number in log file
Code:
 grep -c "$ i CurrDay" attempts.log

.

It is working in linux but solaris 10 I get an error
Code:
 Grep: Re error 41 : No remembered search string. .

Any suggestions for this...

Last edited by nanz143; 07-15-2014 at 11:55 AM..
# 37  
Old 07-16-2014
Quote:
Originally Posted by nanz143
I am not able to print the attempt number in log file
Code:
 grep -c "$ i CurrDay" attempts.log

.

It is working in linux but solaris 10 I get an error
If this line is really "working in Linux", then Linux must be wrong. Compare this line to what i wrote:

Code:
iNrOfAttempts=$(grep -c "$iCurrDay" "$fLog")

"iNrOfAttempts" is a variable, which gets assigned with whatever comes out of the command:

Code:
grep -c "$iCurrDay" "$fLog"

Now, "iCurrDay" is another variable: an integer holding the day of month. This is used as an expression to search a file located at "$fLog", where "fLog" is another variable, supposed to contain a path name pointing to a file. So, filling the line with possible example values, what finally gets executed would read:

Code:
iNrOfAttempts=$(grep -c "28" "/path/to/blabla")

This counts the lines in /path/to/blabla, which contain the string "28" - for the example let us suppose there are 2 - and print that to stdout. The shell now executes whatever is inside "$(...)" and replaces that with the outcome, hence the line finally executed is:

Code:
iNrOfAttempts=2


What you did was to let grep search for:

Code:
grep -c "$ i CurrDay"

which is, because "$" is the regexp for "line end": the end of the line, followed by a space, an "i", another space and the string "CurrDay", which is obviously nonsense. This is why it isn't working - and i'd bet my daily dose of espresso against a single american coffee it isn't working in Lunix either. Because you let it search in a file without a fully qualified path chances are it won't search in the right file either, but that only as an aside.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 38  
Old 07-17-2014
hi i am sorry, there is a typo here.. i typed it from my mobile....
There are no space in the below command.

In Linux:

Code:
grep -c "$iCurrDay" attempts.log

Code:
grep -c "$iCurrDay" /usr/local/wam/scripts/attempts.log

I ran both the commands in the terminal and gave the below output (pasted output from server)

Code:
[v287980@attir50 /]$ grep -c "$iCurrDay /usr/local/wam/scripts/attempts.log 
3

-----

I Also ran from the script

Script
Code:
typeset chDOW="${1:0:3}"
if [ "${chDOW}" != "Thu" ] ; then
     return 1
fi
return 0
}
pCheckAttempts ()
{
typeset fLog="${1}"
typeset iCurrDay="${2}"
typeset iNrOfAttempts=0
typeset iMaxAttempts=3
grep "$iCurrDay" "$fLog" > "${fLog}.tmp"

echo "$iCurrDay   $fLog "
mv "${fLog}.tmp" "${fLog}"
echo "$iCurrDay" >> "$fLog"
iNrOfAttempts=$(grep -c "$iCurrDay" "$fLog")

#echo "No. Of attempts: $iNrOfAttempts "
if [ "$iNrOfAttempts" -gt "$iMaxAttempts" ]
then
     echo "Attempts $iNrOfAttempts exceeds set maximum of attempts $iMaxAttempts"

return 1
fi
return 0
}

# ------- main
typeset chDate="$(date +'%a %d')"
typeset fAttemptsLog="/usr/local/wam/scripts/attempts.log"
if ! pCheckDOW "${chDate:0:3}" ; then
     echo "Error: This runs only on Thursday." >&2
     exit 1
fi
if ! pCheckAttempts "$fAttemptsLog" "${chDate:4:2}" ; then
     echo " Mail sent to IOC, Exiting script"
exit 2
fi
#iall stuff here
echo "1. Attempt $ (grep -c "$iCurrDay" "$fLog")" ## This is not working

echo "2. Attempt $iNrOfAttempts " ## This is not working 

echo "3. Attempt  (grep -c "$iCurrDay" /usr/local/wam/scripts/attempts.log) ## This is working in linux, but not in Solaris 10

echo -e " 1. command issued to bring down apps. \n 2. checking apps"

echo -e " 1. app1 down \n 2. app2 still running"
exit 1
echo -e " 3. app3 down \n app4 down"
exit 0

Output
Code:
[v287980@attir50]$ ./testscript.sh 
17   /usr/local/wam/scripts/attempts.log 
grep: : No such file or directory
1. Attempt no.   
2. No. Of attempts:  
3. Attempt no. 2  
 1. command issued to bring down apps. 
 2. checking apps
 1. app1 down 
 2. app2 still running

I am sure i made a mistake some where, but not able to find that..
# 39  
Old 07-17-2014
Quote:
Originally Posted by nanz143
I am sure i made a mistake some where, but not able to find that..
Your pasted scrpit code code seems to be missing the first few lines, but this is probably a typo too. Also your line:

Code:
[v287980@attir50 /]$ grep -c "$iCurrDay /usr/local/wam/scripts/attempts.log 
3

misses a double quote, but that is probably a typo too.

Your real mistake is not to understand the "scope" of variables. Variables are always bound to the context they are defined in. Outside of this context, they do not exist. (It is possible to overrule this somewhat by using "export", but let us set this aside for the moment.)

consider the following script:

Code:
#! /bin/bash

subfunc1 ()
{
typeset localvar="abc"
echo "inside subfunc1(), value of localvar is: $localvar"
return 0
}


# ------- main
typeset localvar="XYZ"

echo "in the main routine, value of localvar is: $localvar"
subfunc1
echo "back in main, value of localvar is: $localvar"

exit 0

A main routine is executed. This routine calls another routine, "subfunc1()" and - upon entering this function - the context changes. When leaving this function, the context changes again and the original variables are restored.

Now, in light of this, check where "iCurrDay" is defined. You will see that outside the function pCheckAttempts() the value of "iCurrDay" is not defined because the variable inside this function - is INSIDE! And nowhere else!

You might want to play around with the example script above, defining and displaying additional variables at various places, to get comfortable with the concept. In fact this is a very powerful device. With this it is possible to write subfunctions which do not care about their surroundings. (In fact it is strongly advised to write every function this way.) This is part of a general programming concept called "encapsulation". This means that in a program every subfunction should be completely independent of all the other parts in its own operation.

For instance, it is possible to do this (here we come back to the "export" keyword):

Code:
#! /bin/bash

subfunc1 ()
{
typeset localvar="abc"
echo "inside subfunc1(), value of localvar is: $localvar"
echo "inside subfunc1(), value of blabla is: $blabla"
return 0
}


# ------- main
typeset localvar="XYZ"
typeset othervar="blabla"

# export othervar

echo "in the main routine, value of localvar is: $localvar"
echo "                     value of blabla is: $blabla"
subfunc1
echo "back in main, value of localvar is: $localvar"
echo "                     value of blabla is: $blabla"

exit 0

Run this once as it is and then after uncommenting the line "export othervar" and notice the effect. After exporting the variable it becomes visible in subfunc1() too. Do such a thing only with extreme caution, because now the function becomes dependent on something outside. It would not be possible any more to take it out of the script and use it in another script.

I hope this helps.

bakunin

Last edited by bakunin; 07-17-2014 at 10:11 AM..
This User Gave Thanks to bakunin For This Post:
# 40  
Old 07-17-2014
Thanks bakunin, thats a bit techy for me, but will go through and understand... and will get back to you ... SmilieSmilie
# 41  
Old 07-18-2014
Quote:
Originally Posted by nanz143
thats a bit techy for me,
The principle is quite simple: programming means building blocks, from which to build bigger blocks - you build bricks, then walls from bricks, then houses from walls, etc.. If you take care that the brick's function is not depending on assumptions about the wall or the house it is going to be used in then the brick is much more versatile.

For the same reason you build functions in a way that they do not make assumptions about the program they are called from. This way you make your functions much more versatile.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 42  
Old 07-19-2014
Yes.... am learning alot here... also could help me in suggesting me book which will help me building my scripting skills.. Thank you so much...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run script on every last sunday of the month

Hi ALL, I have been testing this script to run for every last Sunday of the month,looks like month which have 5 sunday (july 2016 )is not passing this and failing every time. Here is what I am using, current_date=31 echo " CURRENT DAY -> $current_date" if ... (2 Replies)
Discussion started by: netdbaind
2 Replies

2. Shell Programming and Scripting

How to check if script is run via cron or manual=command line?

Hi all, I have a script that can be run via cron or via the command line. Is there any way that I can place something on the script to be able to distinguish/differentiate whether the script was run via a user in the command line or whether it was run from the cron? (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

Script to run php and check value in txt

Hello, I'm looking for a bash script, that I will run from cron, that executes a php script. After 5 minutes that the php script is executed, the bash script, must check a value in a text file /public_html/check.txt if check.txt = 0 the script will stop, if check.txt is not = 0 it must... (0 Replies)
Discussion started by: andymc1
0 Replies

4. UNIX for Advanced & Expert Users

Autosys job run on Sunday

Hi, I need to create a autosys job which will run on every sunday at 7:30 AM NY time for each 10 min interval of time. days_of_week: su start_mins: 0,10,20,30,40,50 run_window:"07:30" Is it fine? Please help Thanks, Anup (2 Replies)
Discussion started by: anupdas
2 Replies

5. UNIX for Dummies Questions & Answers

Run script on every second sunday

I was to schedule a script in a crontab after every 15 days specically on every 2nd Sunday. I know that i can schedule on basis of weekdays, but can it be done by skipping in between???:wall: (5 Replies)
Discussion started by: masquerer
5 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

Crontab job to run every sunday

Hello, I wanted to run one of my shell script for every sunday at 5PM. Here is the crontab entry i am using.. 00 17 * * 0 /inventory/update.sh > /inventory/update.log 2>&1 The job is not kicking on sunday at the specified time.. Am i missing anthing? Any help is appreciated... (2 Replies)
Discussion started by: govindts
2 Replies

8. Shell Programming and Scripting

Check if script run by a user directly or via other scripts

Hi, i have a script 'a.sh' that should be called only by certain scripts like b.sh, c.sh Inside a.sh, how can i determine 1) if this script was run directly from command prompt (or scheduler) 2) if called via other scripts? Is there an easy way to get parent process name (not just pid),... (2 Replies)
Discussion started by: ysrinu
2 Replies

9. Solaris

How to check for Saturday or Sunday

Hi , I have a date parameter passed in YYYYMMDD format , how can I check whether it is Sat or Sun on Solaris box , as we can do the same easily on linux box by using date -d YYYYMMDD '+a' . Any pointres will be really helpful . (5 Replies)
Discussion started by: harpreetanand
5 Replies

10. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies
Login or Register to Ask a Question