Created
November 4, 2013 21:35
-
-
Save aj0strow/7309559 to your computer and use it in GitHub Desktop.
McGill COMP 206 - Assignment 2 Question 3
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 | |
INSTRUCTIONS=""" | |
USAGE | |
$ compile -o filename [--clean] [--backup] [--archive] [--help] cfilenames | |
WHERE | |
filename: name of the outputed executable | |
cfilenames: list of c source files to be compiled together | |
OPTIONS | |
--help: display this message | |
--clean: deletes all the .o files before compiling | |
--backup: copies all .c and .h files to backup directory before compilation | |
--archive: TARs contents of source dir, moves archive to backup | |
""" | |
instruct() { | |
echo "$INSTRUCTIONS" | |
exit 1 | |
} | |
if test $# -eq 0; then | |
instruct | |
fi | |
CLEAN=false | |
BACKUP=false | |
ARCHIVE=false | |
FILENAME="" | |
CFILENAMES="" | |
while test $# -gt 0 | |
do | |
case "$1" in | |
'-h'|'--help') | |
instruct | |
;; | |
'--clean') | |
CLEAN=true | |
shift 1 | |
;; | |
'--backup') | |
BACKUP=true | |
shift 1 | |
;; | |
'--archive') | |
ARCHIVE=true | |
shift 1 | |
;; | |
'-o') | |
FILENAME="$2" | |
shift 2 | |
;; | |
*) | |
if test -r $1; then | |
CFILENAMES="$CFILENAMES $1" | |
shift 1 | |
else | |
echo "ERROR: \"$1\" not a valid option" | |
instruct | |
fi | |
esac | |
done | |
if test -z "$FILENAME"; then | |
echo "ERROR: no filename provided" | |
instruct | |
elif test -z "$CFILENAMES"; then | |
echo "ERROR: no cfilenames provided" | |
instruct | |
fi | |
if $CLEAN; then | |
rm -f *.o | |
fi | |
if $BACKUP; then | |
mkdir -p backup | |
cp -f *.[ch] backup | |
fi | |
if $ARCHIVE; then | |
mkdir -p backup | |
tar -czf archive.$(date +%Y%m%d-%H%M%S).tgz * | |
mv *.tgz backup | |
fi | |
gcc -c $CFILENAMES && gcc -o $FILENAME *.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment