1

I am trying to write a script which works out who should be doing the cooking in a team of 4. The script takes the week of the year and divides it by 4. If there is no decimal value, it is Sandra's turn. If it contains .25, it is Christina's turn. If it contains .5, it is Zack's turn. If it contains .75, it is Peter's turn.

#!/bin/bash
# Script to check whose turn it is to cook

let week=$(date +%W)
echo "Current week of the year is $week"

let teamsize=4
counter=`echo "$week/$teamsize"|bc -l`
echo "Current week's counter is $counter"

if [[ "$counter" = ^[0-9]*\.0*$ ]]
    then echo "Sandra's turn"
elif [[ "$counter" = ^[0-9]*\.250*$ ]]
    then echo "Christina's turn"
elif [[ "$counter" = ^[0-9]*\.50*$ ]]
    then echo "Zack's turn"
elif [[ "$counter" = ^[0-9]*\.750*$ ]]
    then echo "Peter's turn"
else echo "No cook matched!"
fi

For the current week, it is not matching any of the entries. The week of the year is 18 and this is the output:

Current week of the year is 18
Current week's counter is 4.50000000000000000000
No cook matched!

Appreciate any help figuring out how to match the variable counter to the regex number pattern. Thanks.

4

1 Answer 1

0

This is a shorter version of your script using awk.

awk  -v date=$(date +%W) -v teamsize=4 '
BEGIN{ 
    turn= sprintf("%.2f",date / teamsize);
    if( turn ~ /^[0-9]*\.0*$/ ) { print "Sandra'"'"'s turn" }
    else if( turn ~ /^[0-9]*\.250*$/ ) { print "Christina'"'"'s turn" }
    else if( turn ~ /^[0-9]*\.50*$/ ) { print "Zack'"'"'s turn" }
    else if( turn ~ /^[0-9]*\.750*$/ ) { print "Peter'"'"'s turn" }
    else { print "No cook matched!" }
}'

Hope its useful for you.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.