Created
May 14, 2020 04:40
-
-
Save biggles5107/dd9b445439c153ed9ebf31ff8ceaf358 to your computer and use it in GitHub Desktop.
autohwsched.sh: a simple script to generate class schedule list for each day in a month in a CSV format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## autohwsched.sh: a simple script to generate class schedule list for each day in a | |
## month in a CSV format. It is useful for tracking at a glance whether and how | |
## consistently daily work is being completed. | |
## usage: ./autoshwsched.sh month > file.csv | |
## NOTE: This script assumes a five day class schedule and reads from "classes.csv" in | |
## the same directory by default (can be configured below). It does not add any | |
## formatting data, and currently does not remove class lists for partial weeks at the | |
## beginning and end of the month. Some manual editing is required on the output file. | |
# Copyright (C) 2020 Christian Arnold | |
# | |
# Redistribution and use in source and binary forms, with or without modification, are | |
# permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, this list | |
# of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
# of conditions and the following disclaimer in the documentation and/or other materials | |
# provided with the distribution. | |
# | |
# 3. Neither the name of the copyright holder nor the names of its contributors may be | |
# used to endorse or promote products derived from this software without specific prior | |
# written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | |
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
# DAMAGE. | |
MONTH=$1 # first argument (required); later arguments are ignored | |
# if first argument is not given, give usage and exit | |
[ -z "$MONTH" ] && echo "usage: $0 [4mmonth[0m > [4mfile.csv[0m" >&2 && exit 1 | |
# class schedule to read from | |
FILE=classes.csv | |
# check whether the file actually exists or not | |
[ ! -x $FILE ] && echo "$0: need to create $FILE file to generate calendar from" >&2 && exit 2 | |
# Our iterator which we will use later | |
i=0 | |
# Read the output from `cal' and cycle through each line one by one for processing | |
cal -m $MONTH | while read -r LINE; do | |
# break if the line is empty because that could cause problems | |
[ -z "$LINE" ] && break | |
# increment the iterator (it's probably better to use a for loop for this...) | |
i=$((i+1)) | |
# Trim the trailing whitespace of the first line and skip | |
# everything else. Our read command will ignore leading whitespace. | |
[ $i -eq 1 ] && sed -e 's/[[:space:]]*$//' <<< "$LINE" && continue | |
# The second line contains only names of days so we can simply echo the line | |
# we already edited and continue | |
[ $i -eq 2 ] && sed -e 's/[[:space:]]*$//' <<< "$LINE" | tr " " "," && continue | |
# The third line is where the actual calendar starts. Rather than try to count | |
# the whitespace, we are going to count the number of days in the first week | |
# (it is usually less than 7) so we simply subtract that number from 7 and add | |
# that number of commas to the beginning so that it is aligned properly in | |
# Excel or whatever | |
if [ $i -eq 3 ]; then | |
DAYS_OF_FIRST_WEEK=`wc -w <<< "$LINE"` | |
#echo debug: DAYS_OF_FIRST_WEEK=$DAYS_OF_FIRST_WEEK >&2 | |
if [ $DAYS_OF_FIRST_WEEK -lt 7 ]; then | |
EXTRA_FIELDS=$((7-DAYS_OF_FIRST_WEEK)) | |
#echo debug: EXTRA_FIELDS=$EXTRA_FIELDS >&2 | |
for j in `seq $EXTRA_FIELDS`; do | |
LINE=",$LINE" | |
done | |
fi | |
#echo debug: line = $LINE >&2 | |
fi | |
# Remove trailing whitespace, condense double spaces into a single space, | |
# and replace spaces with commas | |
sed -e 's/[[:space:]]*$//' <<< "$LINE" | tr -s '[:space:]' | tr " " "," | |
# After we have parsed the line of date numbers, we are simply going to be | |
# pasting in the csv file defined earlier. We could delete the items we don't | |
# have days for (like in the first and last week of the month) but that's way | |
# easier to do manually after the fact than try to code for it here... | |
# We are also going to add an extra field to the beginning because | |
# weekend classes aren't a thing. So this requires a bit of editing. But note | |
# that we are doing no other editing to the file, so we are assuming that the | |
# file is in proper csv format. | |
while read -r line; do | |
echo ",$line" | |
done < $FILE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment