Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- cmd=$1
- file=$2
- #takes filename as argument and returns top and bottom result seperated by colon
- topAndBotGrade() {
- #top is the highest grade
- highestGrade=`awk -F":" '{ print $3 }' $file| sort -nr | head -1`
- #bottom is the lowest grade
- lowestGrade=`awk -F":" '{ print $3 }' $file| sort -nr | tail -1`
- #echo $highestGrade
- echo $highestGrade":"$lowestGrade
- }
- #outputs the amount of students with grades below 50
- Failed() {
- #failed is the number of students with grades below 50
- failed=`awk -F":" '$3<50' $file | awk '{print NR}' | tail -1`
- echo $failed
- }
- #shows all the students with the highest marks
- Top() {
- top=`awk -F":" '{ print $3 }' $file | sort -nr | head -1`
- echo -e "`awk -F":" '($3 == top) { print $2 }' top=$top $1`"
- }
- #outputs the average mark
- Avg(){
- total=0
- count=0
- mark=0
- while read line
- do
- #sets current mark the loop is looking at
- $mark=`echo $line | awk -F":" '{ print $3 }' $file`
- #echo $mark
- #adds mark to the total
- $total=`echo $(($total+$mark)) | bc`
- #echo $total
- #increases counter
- $count=$(($count+1))
- done <$file
- echo "scale=2; $total/$count" | bc
- }
- #make sure the input file exists
- if [ ! -f $file ]
- then
- echo "Input file [$file] not found - Aborting"
- exit
- fi
- if [ "$1" == m ]
- then
- topAndBotGrade $file
- elif [ "$1" == f ]
- then
- Failed $file
- elif [ "$1" == n ]
- then
- Top $file
- elif [ "$1" == a ]
- then
- Avg $file
- else
- #error trapping.
- echo "Invalid input."
- fi
Add Comment
Please, Sign In to add comment