HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashMinor

Create and open a file to take lecture notes depending on day and time

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lecturefilecreatetakeopendependingtimenotesandday

Problem

I am just wondering what, if any, more elegant ways might exist to write a script to accomplish the task that the script below accomplishes. The script works, so I don't need it to be debugged, but I want to learn more. Other commands to be used, other ways of structuring it, etc. I have taken it through shellcheck.

Yesterday I decided to try my hand at a shell script that would create and open a file to take notes for each of my classes in the folder designated for notes from that class. It was pretty satisfying to do; I started with basically no knowledge and over the course of an hour or two I went from a script that would prompt me to enter the class and the desired file title (read Class read Title if [[ $Class = "X]] then touch /path/"$Title".docx etc) to something that would just create the file in the right place based on the time/date the command is invoked, since the title is always today's date anyhow.

Again, it works. I just know that there must be more ways to skin this cat and I was wondering what those might be. The version below has been revised once after input from some good samaritans.

Also, FWIW, this is running on OS X Sierra.

#!/bin/bash

read date hour day month <<< $(date +"%-d %-H %a %B")
if [[ $hour -lt 11 && $day = "Tue" ]] || [[ $hour -lt 11 && $day = "Thu" ]]; then
    dir="/path/to/first"
elif [[ $hour -gt 12 && $hour -lt 15 && $day = "Tue" ]] || [[ $hour -gt 12 && $hour -lt 15 && $day = "Thu" ]]; then
    dir="path/to/second"
elif [[ $hour -ge 16 && $hour -le 17 && $day = "Wed" ]]; then
    dir="/path/to/third/"
else
    echo "I am of no use to you right now"
    exit 1
fi
file="$dir/$month $date".docx
touch "$file"
open "$file"
killall Terminal

Solution

killall Terminal is very harsh! Maybe you aren't a serious Terminal user now, but someday, you might be doing something important in another Terminal window. And, due to the way macOS works, multiple windows all run under a single Terminal process, so you would be killing all of your Terminal windows. At most, do

osascript -e 'tell application "Terminal" to close the front window'


… which sends a message to just one window using AppleScript. But I would consider even that to be too violent for my taste.

I find those cases too cumbersome to read as conditions. I would write out all of the combinations, using pattern matching, and in particular, extended pattern matching enabled by shopt -s extglob.

#!/bin/bash

shopt -s extglob

read date hour day month <<< $(date +"%-d %-H %a %B")

case $day.$hour in
    @(Tue|Thu).@(8|9|10|11))
        dir="/path/to/first"
        ;;
    @(Tue|Thu).@(13|14))
        dir="/path/to/second"
        ;;
    Wed.16)
        dir="/path/to/third"
        ;;
    *)
        echo "I am of no use to you right now"
        exit 1
esac
…

Code Snippets

osascript -e 'tell application "Terminal" to close the front window'
#!/bin/bash

shopt -s extglob

read date hour day month <<< $(date +"%-d %-H %a %B")

case $day.$hour in
    @(Tue|Thu).@(8|9|10|11))
        dir="/path/to/first"
        ;;
    @(Tue|Thu).@(13|14))
        dir="/path/to/second"
        ;;
    Wed.16)
        dir="/path/to/third"
        ;;
    *)
        echo "I am of no use to you right now"
        exit 1
esac
…

Context

StackExchange Code Review Q#159284, answer score: 2

Revisions (0)

No revisions yet.