#!/bin/sh -u # Answer file: Exercise #5 - gethangman.sh #------------------------------------------------------------------ # fetch, decompress many times, and expand the mystery .hangman file #------------------------------------------------------------------ # Syntax: $0 (no arguments) #------------------------------------------------------------------ # Purpose: # Fetch the hangman mystery file from # http://teaching.idallen.com/dat2330/04f/notes/.hangman # Decompress it several times. Expand the tar archive. # Do not overwrite any existing files. # Quick-exit the script non-zero if anything goes wrong along the way. # Optional: fix the permissions in the hangman directory. #------------------------------------------------------------------ # Student Name: Ian Allen # Algonquin EMail Address: alleni # Student Number: 074-210-779 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian Allen # Assignment Name/Number/Date: Exercise #5 due December 7 # Comment: This is a sample solution. #------------------------------------------------------------------ # Set a standard shell search path and a friendly umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # The name of the file we are fetching - used all through the script. # The name of the directory inside the tar file; do not overwrite it. # filename='.hangman' tardir='hangman' # Validation Section # current dir must have write and execute permissions # if [ ! -w . -o ! -x . ] ; then echo 1>&2 "$0: current directory . missing write or execute permissions" exit 1 fi # do not overwrite any existing pathnames in current directory # if [ -e "$filename" ] ; then echo 1>&2 "$0: a pathname '$filename' already exists here; nothing done" exit 2 fi if [ -e "$tardir" ] ; then echo 1>&2 "$0: a pathname '$tardir' already exists here; nothing done" exit 3 fi # Processing Section # fetch the mystery file and make sure the fetch got a non-empty file # wget http://teaching.idallen.com/dat2330/04f/notes/"$filename" || exit 4 if [ ! -s "$filename" ]; then echo 1>&2 "$0: the downloaded '$filename' file is missing or empty" echo 1>&2 "$0: nothing done" exit 4 fi # Do the correct series of decompress steps to get to the tar archive. # mv "$filename" "$filename.bz2" || exit 5 bunzip2 "$filename.bz2" || exit 5 mv "$filename" "$filename.gz" || exit 5 gunzip "$filename.gz" || exit 5 mv "$filename" "$filename.bz2" || exit 5 bunzip2 "$filename.bz2" || exit 5 mv "$filename" "$filename.gz" || exit 5 gunzip "$filename.gz" || exit 5 mv "$filename" "$filename.bz2" || exit 5 bunzip2 "$filename.bz2" || exit 5 mv "$filename" "$filename.bz2" || exit 5 bunzip2 "$filename.bz2" || exit 5 mv "$filename" "$filename.gz" || exit 5 gunzip "$filename.gz" || exit 5 # Expand the tar archive into the current directory. # Make sure a $tardir directory results. # Remove the tar file. # tar xvf "$filename" || exit 6 if [ ! -d "$tardir" ] ; then echo 1>&2 "$0: cannot find '$tardir' after expanding '$filename'" exit 7 fi rm "$filename" echo "The '$tardir' directory is ready for you: cd $tardir" # Optional: Add write permissions for owner. # (Modifying the code is difficult if write permissions are missing.) chmod u+rwx "$tardir" || exit 8 chmod u+rw "$tardir"/* || exit 8 # script exits with the return code of the last command executed