So my script has to calculate day when someone will be back from trip. For calculating using user input for leaving day and length of trip. Everyday has certain number like Monday-1; Tuesday-2 etc but it gets complex when user input for Sunday must be 0.
Script atm what I have made:
#!/bin/bash
#Data
echo -e "\e[00;31mRemember!\e[00m Sunday-0; Monday-1; Tuesday-2; Wednesday-3; Thursday-4; Friday-5; Saturday-6;"
echo
echo -n "What day will you leave? "
read left
echo -n "How long is your trip? "
read days
#Calculating
cb=`echo "$left+$days" | bc`
comeback=`echo "$cb % 6" | bc`
#Output
if [ $comeback = 0 ]; then
echo "You will be back at Sunday"
fi
if [ $comeback = 1 ]; then
echo "You will be back at Monday"
fi
if [ $comeback = 2 ]; then
echo "You will be back at Tuesday"
fi
if [ $comeback = 3 ]; then
echo "You will be back at Wednesday"
fi
if [ $comeback = 4 ]; then
echo "You will be back at Thursday"
fi
if [ $comeback = 5 ]; then
echo "You will be back at Friday"
fi
if [ $comeback = 6 ]; then
echo "You will be back at Saturday"
fi
My problem is that It never calculates right when trip is over a week or when it should end on Sunday. I got my script working when I started counting like Sunday-1; Monday-2 etc but Sunday must be 0.
To correct your script:
comeback=$(echo "(${left} + ${days}) % 7" | bc)
Also, you should be using an array for output:
days_of_week=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
echo "You will be back on ${days_of_week[${comeback}]}"
I also want to point out a fun answer. With date
from GNU coreutils, you can do this after you read $left
and $days
:
echo "You will be back on $(date -d "${days_of_week[${left}]} + ${days} days" +%A)"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.