1748104 Members
4914 Online
108758 Solutions
New Discussion юеВ

Scripting problem

 
SOLVED
Go to solution
dictum9
Super Advisor

Scripting problem

I have a problem I need to script -

there are 4 users and one of them is to be selected each week, like this:

week 1: user1
week 2: user2
week 3: user3
week 4: user4
week 5: user1
week 6: user2

and so on, repeating user1, user2, user3 and user4.

Any idea how to script this?
7 REPLIES 7
Jonathan Fife
Honored Contributor
Solution

Re: Scripting problem

I don't know how you're determining the week, but for a static list:

for i in 1 2 3 4 5 6 7 8 9; do
echo week $i: user$(($(((i - 1) % 4)) + 1));
done
Decay is inherent in all compounded things. Strive on with diligence
Jeff_Traigle
Honored Contributor

Re: Scripting problem

One method...

#!/usr/bin/sh

typeset -i WEEK=1
typeset -i USERNUM=1

while true
do
for USERNUM in 1 2 3 4
do
echo "week ${WEEK}: user${USERNUM}"
WEEK=$((${WEEK}+1))
done
done
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: Scripting problem

Hi:

If I understand correctly, you could use the week number (see the 'date' manpages) to rotate through. Depending on your choice of when a week begins (Sunday or Monday) use either:

# date +%U
# date +%W

For example:

# let X=$(date +%W)%4;echo ${X}
2

...or the second group. More directly based on your example this is simply week-34.

# echo $(date +%W)
34

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting problem

Using date +%U, +%V, or +%W can give somewhat screwy results at the end/beginning of the year. I assume that you will run some sort of cronjob at the same time each week.

----------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin
typeset -i WK=0
typeset USER=""
WK=$(( (($(caljd.sh) + 1) / 7) % 4 ))
((WK += 1))
U="user${WK}"
echo "User for this week is ${U}"
exit 0
------------------------------------------
Use the attached script, caljd.sh to solve virtually any date problem you can imagine. Invoke as caljd.sh -u for full usage.


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

Re: Scripting problem

OK.. The week begins on Fridays.
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting problem

In that case change this line:

WK=$(( (($(caljd.sh) + 1) / 7) % 4 ))

to

WK=$(( (($(caljd.sh) - 4) / 7) % 4 ))

and your weeks will begin on Friday.
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: Scripting problem

Just being a little silly, but here is a direct calculation using perl:

For any given time is it run it will print the corresponding cycle number, starting a fresh one every Friday.
Optionally provide it a time (unix time in seconds :^) and it will print the cycle for that time.

Suggested solution:

perl -le '$t=shift||time;$t+=86400*((11-(localtime($t))[6])%7);$w=int($t/(7*86400)); print 1+$w%4'

So it takes a time $t from clock or optional argument.
Adds to that a number of days worth of seconds to get to the next Friday.
It does that by taking the day-of-the-week number returned by localtime in its element 6 and subtracting that from 11 modulo 7.
Next divide by the number of seconds in a week to get a (psuedo) week number in $w
Finally a 'mod 4' on that give the user number.
To make a week start on Saturday, change the 11 to a 12.


btw...
To assure me of the math, and to get sample times I used:

Suggested verification:

perl -le '$x=time; for (1..40){$x+=86400;$t=$x+86400*((11-(localtime($x))[6])%7);$w=int($t/(7*86400));$u=1+$w%4; print "$u $x $t ", scalar localtime($x)}'

This generates:
2 1188011902 1188530302 Fri Aug 24 23:18:22 2007
2 1188098302 1188530302 Sat Aug 25 23:18:22 2007
:
2 1188530302 1188530302 Thu Aug 30 23:18:22 2007
3 1188616702 1189135102 Fri Aug 31 23:18:22 2007
3 1188703102 1189135102 Sat Sep 1 23:18:22 2007
:
3 1189135102 1189135102 Thu Sep 6 23:18:22 2007
4 1189221502 1189739902 Fri Sep 7 23:18:22 2007


Grins,
Hein.