Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # calcweek.sh by dan saunders <[email protected]>
- # Define some defaults
- SEASONS="spring summer fall winter"
- YEAR="$(date +%Y)"
- DAY="$(date +%j)"
- # If there is a first argument, make sure it's a season
- if [[ -n "$1" ]] && [[ "$SEASONS" =~ "$1" ]]; then
- SEASON="$1"
- # Check for a second argument - a date input
- if [[ -n "$2" ]]; then
- # Do some dirty cleverness by throwing the input through date - it will complain if it is an invalid format ;)
- date +%m/%d/%Y -d "$2" > /dev/null 2>&1
- [[ "$?" -ne "0" ]] && echo "That's not a valid date format, silly!" && exit
- # Extract that stuff!
- DATE=$(date +%m/%d/%Y -d "$2" 2>/dev/null)
- YEAR="$(date -d "$DATE" +%Y)"
- DAY="$(date -d "$DATE" +%j)"
- fi
- else
- # Ok, so they didn't provide a date. Time to use trial and error to search for the correct semester
- # In order to loop through a sequence of words, I had to do some dirty work using read
- SEASON=$(echo $SEASONS | while read line; do
- for word in $line; do
- [[ -f "./$word$YEAR.conf" ]] && source "./$word$YEAR.conf"
- [[ -f "/var/public/fall2015/unix/sfp0/$word$YEAR.conf" ]] && source "/var/public/fall2015/unix/sfp0/$word$YEAR.conf"
- [[ -z "$SEMESTER" ]] && continue
- if [[ "$SEMESTER_START" -le "$DAY" ]] && [[ "$DAY" -le "$SEMESTER_END" ]]; then
- echo $word
- break
- fi
- done
- done)
- fi
- # Load the configuration, or at least attempt to
- [[ -f "./$SEASON$YEAR.conf" ]] && source "./$SEASON$YEAR.conf"
- [[ -f "/var/public/fall2015/unix/sfp0/$SEASON$YEAR.conf" ]] && source "/var/public/fall2015/unix/sfp0/$SEASON$YEAR.conf"
- [[ -z "$SEMESTER" ]] && echo "Could not find configuration for \"$SEASON$YEAR\". Maybe you're looking into the future?" && exit
- # Is the date during the given semester?
- if [[ "$DAY" -le "$SEMESTER_START" ]]; then
- # It's before, so we must return 0
- echo 0
- exit
- elif [[ "$SEMESTER_END" -le "$DAY" ]]; then
- # It's after, so we must return 16
- echo 16
- exit
- fi
- # Check for break weeks. Since bash doesn't have a loop by index feature, use cleverness to make it
- for i in "${!BREAK[@]}"; do
- # Checks every other index to determine if it's a break week; the even and odd indexes are the same - it's not one
- if [[ $((i%2)) -eq 0 ]] && [[ "${BREAK[$i]}" -le "$DAY" ]] && [[ "$DAY" -le "${BREAK[$((i+1))]}" ]] && [[ "${BREAK[$i]}" -ne "${BREAK[$((i+1))]}" ]]; then
- echo break week
- exit
- fi
- done
- # The final step, display the week number, and in the process remove leading zeroes that we don't need
- echo $(($(date -d "$(date -d "$YEAR-01-01 + $(($DAY - 1)) days")" +%V) - $STARTWEEK))
Advertisement
Add Comment
Please, Sign In to add comment