#!/bin/bash _IFS="$IFS" # save IFS IFS="$_IFS:" # append colon to IFS syntax () { echo "Syntax: ${0##*/} [-es] file..." exit 1 } # # set output option: 0=standard 1=extended # output=0 # default when option is not specified OPTERR=1 # verbose getopts errors while getopts 'es' opt do case $opt in (s) output=0; shift;; (e) output=1; shift;; (*) syntax;; # illegal option esac done # globals declare -i files i j s exam ghd gd gc gp gpc gf average total # # test files # files=0 if [ $# -gt 0 ] then for f in $@ do if [ -f $f ] then if [ -r $f ] then if [ -s $f ] then let files+=1 else echo file $f is empty fi else echo file $f is not readable fi else echo file $f does not exist fi done if [ $# -ne $files ] then echo File errors. Abort. exit 1 fi else echo No data files specified. syntax fi # # functions # rname () { echo '?' } exe_marks () { i=0 j=0 for i in ${exe[@]} do let j+=i done if [ $j -gt 10 ] then j=10 fi echo $j } total_marks () { i=0 j=0 for i in ${as[@]} ${exe[@]} $exam do let j+=i done if [ $j -gt 100 ] then j=100 fi echo $j } grade () { if [ $total -ge 85 ] then let ghd+=1 echo HD $ghd elif [ $total -ge 75 ] then let gd+=1 echo D $gd elif [ $total -ge 65 ] then let gc+=1 echo C $gc elif [ $total -ge 50 ] then let gp+=1 echo P $gp elif [ $total -ge 45 ] then let gpc+=1 echo PC $gpc else let gf+=1 echo F $gf fi } # # main loop # for f in $@ do ghd=0 gd=0 gc=0 gp=0 gpc=0 gf=0 average=0 while read snum uname scores do i=0 s=0 exam=0 total=0 declare -a as exe for s in $scores do if [ $i -le 4 ] then as[${#as[@]}]=$s elif [ $i -le 15 ] then exe[${#exe[@]}]=$s elif [ $i -eq 16 ] then exam=$s fi let i+=1 done exemarks=$(exe_marks) total=$(total_marks) grade=$(grade) echo -e "$snum\t$(rname)\t${as[@]}\t$exemarks\t$exam\t$total\t$grade" unset as exe done < $f if [ $output -eq 1 ] then echo Grades: HD $ghd D $gd C $gc P $gp PC $gpc F $gf echo Average: $average fi done