diff --git a/Labb5/ovning4.sh b/Labb5/ovning4.sh index 9068408..a605bb7 100755 --- a/Labb5/ovning4.sh +++ b/Labb5/ovning4.sh @@ -5,14 +5,85 @@ # Övning 4, labb 5 # Grade-calculation script -# Set some variables +# Set some defaults G=30 VG=48 Form=form.txt +# Function to print the students grade and points +print_grade() +{ + echo "Your total points on the test was: $Total" + + if [ $Total -lt $G ]; then + echo "Your grade is: IG" + elif [ $Total -ge $VG ]; then + echo "Your grade is: VG" + else + echo "Your grade is G" + fi + + exit 0 +} + +# Define help functions +short_help() +{ + printf "Usage: `basename $0` -f " + printf " -o -G -VG \n" +} + +long_help() +{ + short_help + cat <<- EOF + -f Optional filename to read the form data from. + -o Optional output file for the points and grade. + -G Number of minimum points for a grade G (Default is 30). + -VG Number of minimum points for a grade VG (Default is 48). + EOF +} + +# Pass command line arguments +while [ -n "$1" ]; do + case "$1" in + -f) Arg="$2" + if [ ! -r $Arg ]; then + echo "Can't read $Arg" > /dev/stderr + short_help + exit 2 + fi + Form="$Arg" + shift 2 + ;; + -o) Outfile="$2" + if [ ! -w `dirname $Outfile` ]; then + echo "You can't write to `dirname $Outfile`"\ + > /dev/stderr + exit 2 + fi + shift 2 + ;; + -G) G=$2 + shift 2 + ;; + -VG) VG=$2 + shift 2 + ;; + -h) long_help + exit 0 + ;; + *) short_help + exit 2 + ;; + esac + # shift +done + # Check if the file exist and that we can read it if [ ! -r $Form ]; then echo "Can't read $Form" > /dev/stderr + short_help exit 2 fi @@ -44,14 +115,8 @@ for i in ${Points[*]}; do done # Print the total points and grade -echo "Your total points on the test was: $Total" - -if [ $Total -lt $G ]; then - echo "Your grade is: IG" -elif [ $Total -ge $VG ]; then - echo "Your grade is: VG" +if [ -n $Outfile ]; then + print_grade > $Outfile else - echo "Your grade is G" + print_grade fi - -exit 0