Class Notes

Automation

Yeah. You should try. It makes your life better. Be warned though [ TODO : An article about airplane user interface and the role of automation ] .

More work than needed

The main goal of automation is to release us from manual work, avoiding repetition, following DRY principle. I have seen opportunity virtually everywhere…..I’m applying it to my homework

1. Generating html [ script 1].

Web technology class requires me to write a lot of htmls and I’m lazy so I sought out to generate template html each homework. Think about it. Would you bother type tags by tags, better yet type and copy and paste ? Or would you rather type: run script and an html pops out of thin air ? Alright, option 2 please . Did you just say script ? Yes. a viable option would be bash from terminal – the evil minion.

I want to do the following : script -dest( this can be defaulted to the current working directory ) /path/to/destination -title title -name name_of_the_generated_file. Somebody on the Internet recommends using getopts, so I slapdash into the following code:

#!/bin/bash

OPTIND=1 

DEST=${PWD}
NAME="dummy"
TITLE="Hello"

echo "Hello World. Path is ${DEST}"

while getopts ":d:n:t:" opt; do

    case $opt in
	d)
	    echo "Setting destination to $OPTARG" >&2
	    DEST=$OPTARG
	    ;;
	n)
	    echo "name of the html is set to $OPTARG" >&2
	    TITLE=$OPTARG
	    ;;
	t)
	    echo "title of the html is set to $OPTARG" >&2
	    NAME=$OPTARG
	    ;;
	
	\?)
	    echo "Invalid option: -$OPTARG" >&2
	    exit 1
	    ;;
	':)' #comment this, because wordpress renders this as a smiley face
	    echo "Option -$OPTARG requires arguments." >&2
	    exit 1
	    ;;
    esac	   
done

shift $((OPTIND-1))

echo "DEST=$DEST"
echo "NAME=$NAME"
echo "TITLE=$TITLE"

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">
  <html>
     <head>
        <title>$TITLE</title>
     </head>
     <body>
     </body>
  </html>" > $DEST/$NAME.html

The minor problem with getopts is that it does not work with long –option parameters ( Why is this a problem ? what about user pass in -d thinking it would be directory but it would delete ??? I don’t know anything is possible ). I can live with that. The main problem is that sometimes it takes in wrong value for name and title which I cannot tolerate. Google-ing around again to find another solution.

2. Script 2. Slightly better

In bash, to reference parameteres/options ( separated by space ), we could do ‘$#’. For example $1 is param 1, $2 is param 2, etc. I do not understand this fully yet ( It would require yet another blog post ).  The main loop would be something like this :

while [[ $# > 1 ]]
do
    key="$1"
    case $key in
	-dest)
	    DEST="$2"
	    shift
	    ;;
	-title)
	    TITLE="$2"
	    shift
	    ;;
	-name)
	    NAME="$2"
	    shift
	    ;;
	*)
	    ;;
    esac
    shift
done

You might have guessed the problem with this script ? It does not work with a list of parameters passed in as a string. For example : script -title “hello, there. im homework” would not work. Well, I could live with that. Will refactor it later.

3. Invoking other command.

A student asks whether he should put his codes in a folder called ‘hw’ but then anybody knowing his email would be able to access his website to which the TA responds that one should hash the folder. For the first homework, I actually used online tool to hash ‘hw#’ into a string as a name to create a folder with. Since I know Unix has built-in hash tool , I could just call it? ( I only create a hashed-string folder on the server not on my computer.) to create a folder called ‘hashed-string-fromed-passed-in-homework-parameter ( from the name parameter )’ then I just copy + paste it into the grading server for grading.

FOLDER_HASH=$(echo -n $FOLDER | /usr/bin/shasum -a 256 | awk '{print $1}') #only print the string before '-'
#shasum called through pipe
....
mkdir -p $DEST/$FOLDER_HASH
....
#Then change echo location a bit
echo "..." > $DEST/$FOLDER_HASH/$NAME.html

However, the generated string is too long🙂

Too long hash

 

Fixed it by using string slicing

FOLDER_HASH=${FOLDER_HASH:0:10}
Fixed it

4. Easy to adapt into other generated homework

Tex stuff .🙂 . Pasting this at the end :

echo "\documentclass{article}
\begin{document}
Student : DUC HO\\\\
\end{document}"> $DEST/$NAME.tex

Goal for next time : Write script to automate uploading through ssh to grading server. Fix the “string sparated by space” as one parameter to the script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s