Scripting problems

Hello,

Im trying to write a script where it will only execute on a certain day. What would the script look like?

if [ today == monday ]

then do this

is this correct??

And you don't want to use cron?

yes cron will be set to run on say monday but if it isnt monday the script wont run

I don't understand. If your crontab line looks like this:

# Minute Hour DayOfMonth MonthOfYear DayOfWeek(00=Sunday)
00 10 * * 01 yourscript

Then your script will allways only run on monday at 10am.

yeah, but I think the OP also wants to enable 'non-monday' logic into the script as well so that 'nothing happens' if the script is ran on monday from the shell.

if my crontab is set up to run only on the 13th.

0 9 13 * * /script

then my script will need to be setup as - if Monday is the 13 then run the script ifMonday is not the 13th not dont do anything, my question is what will my script look like?

if tuesday is 13 ?

then (run script) ?

else do nothing ?

This should do it:

if [ $(date +%A) == Monday ]
then
    echo "Can't run on Mondays"
    exit 2
fi

However, in light of this:

I think that the OP has a misunderstanding of how cron works.

My example does not make any distinction on when a Monday falls in a month. My example says run every monday. The asterisk in the crontab's DayOfMonth field says run everyday and then the DayOfWeek is further interpreted to determine which day/days of the week should it run.

Vgresh99 makes a good point so if you want to limit the script from running interactively on Mondays, implement the first example.

I hope this helps

to check if today is 'monday'....

#!/bin/ksh

# set your 'NOT to run day' here: Sun...Sat [1..7]
# 2 is Monday
not2run='2'

if (( $(date +%u) == not2run )) ; then
   echo "Cannot run today"
else
   echo "Can run today"
fi

or as tmarikle has suggested - must have been typing at the same time.

My system shows that Monday = 1 when I run man strftime.

Thanks to everyone for the info.

Im fairly new to scripts what does the %a stand for? id there anyway to simplify the if condition such as:

if today=tuesday
then run myscript

different locale I guess:

     %u        Weekday  as  a  decimal  number  [1,7],   with   1
               representing Sunday.

I referenced %A, which is different then %a.
When I look up date in the man pages, +format refers me to strftime(3C). man strftime yields:

...
     %a    Locale's abbreviated weekday name.

     %A    Locale's full weekday name.
...

My earlier example is as abbreviated as you probably want but:

if [ $(date +%A) == Monday ]
then
    echo "Can't run on Mondays"
    exit 2
fi

could be written as this but it's less intuitive if you are new:

[ $(date +%A) == Monday ] && exit 2

It looks like you are right about Locale; I guess I wouldn't want to get used to scripting tests using 1=Monday or 2=Monday without knowing my system's locale if Monday was a bad day to run scripts. :smiley:

This change was introduced by XPG4. 1=Sunday is the old way and 1=Monday is the new way. From the Solaris 9 strftime man page:

     The conversion specification  for  %u  was  changed  in  the
     Solaris 8 release. This change was based on the XPG4 specif-
     ication.

I think that HP-UX made the change at 11.0.

Ah, I see - I tested it on Solaris 7
Good to know - thanks.