Operating System - HP-UX
1834459 Members
2566 Online
110067 Solutions
New Discussion

Re: Script - even or odd weeks

 
SOLVED
Go to solution
Andi Rigauer
Regular Advisor

Script - even or odd weeks

Hi , I'd need a function in a normal shell who covers the even or odd issue.
so simple like this:

if today = an even week do this
else
do that
fi

thanks in advance
Andi
god, root where's the difference
10 REPLIES 10
Bernhard Mueller
Honored Contributor

Re: Script - even or odd weeks

What about
date +%U

Regards,
Bernhard
Jean-Luc Oudart
Honored Contributor

Re: Script - even or odd weeks

You can use "date +%V"
test it if you can divide by 2

Rgds
JL
fiat lux
Christian Gebhardt
Honored Contributor
Solution

Re: Script - even or odd weeks

Hi

Try this:

#!/bin/sh
typeset -i week check
week=`date +'%V'`
check=$week/2*2
if [ "$week" = "$check" ]
then
echo "even"
fi

You can also use %U

Chris
Piergiacomo Perini
Trusted Contributor

Re: Script - even or odd weeks

Maybe it's better use

date +%w

the output is weekday as a one-digit decimal number [0-6 (Sunday-Saturday)].

HTH
Piergiacomo Perini
Trusted Contributor

Re: Script - even or odd weeks

i beg your pardon....
i confuse
week with weekday!!!!!

sorry
Bernhard Mueller
Honored Contributor

Re: Script - even or odd weeks

Andi,

Jan-Luc and Christian are correct, to comply with DIN standards it should be %V (week 1 of the year is the first week containing a thursday, otherwise it still belongs to last year).

Regards,
Bernhard
Graham Cameron_1
Honored Contributor

Re: Script - even or odd weeks

Depends on what you mean by odd or even.

date +%U and date +%V both return a week number, the first assumes the week starts on a Sunday, the second assumes Monday.

Pick the one which suits you and then do a simple modulo operation.

eg
#/bin/sh
w=$(date +%U)
(( even=$w % 2 ))
if [ $even -eq 0 ]
then
do this
else
do that
end if

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Graham Cameron_1
Honored Contributor

Re: Script - even or odd weeks

sorry typo, for "end if", read "fi"
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Bruno Ganino
Honored Contributor

Re: Script - even or odd weeks

You can use a option that give to Output the number of week in the year, from 0 to 53.
(the first day of the week is Monday, then all days that preceding the first Sunday of new year, are considered in week number 0)

date +%W

HTH
Bruno

Torino (Turin) +2H
A. Clay Stephenson
Acclaimed Contributor

Re: Script - even or odd weeks

If your real goal is to do something every other week whether or not the beginning week is 0 or 1 thgen the easy method is to use caljd.sh. Without arguments it outputs the truncated Julian Day --- days since 4713BCE.

if [[ $(( (($(caljd.sh) + 1) / 7) % 2) )) -eq 0 ]]
then
echo "Week is even"
else
echo "Week is odd"
fi

This will work whether there are 52 weeks or
53 weeks in the year.

We add 1 to the Julian Day (so that weeks start on Sunday), divide by 7 to get the week number and then a mod 2.

If it ain't broke, I can fix that.