Guest User

Untitled

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #!/bin/bash
  2. cmd=$1
  3. file=$2
  4. #takes filename as argument and returns top and bottom result seperated by colon
  5. topAndBotGrade() {
  6. #top is the highest grade
  7. highestGrade=`awk -F":" '{ print $3 }' $file| sort -nr | head -1`
  8. #bottom is the lowest grade
  9. lowestGrade=`awk -F":" '{ print $3 }' $file| sort -nr | tail -1`
  10. #echo $highestGrade
  11. echo $highestGrade":"$lowestGrade
  12. }
  13.  
  14. #outputs the amount of students with grades below 50
  15. Failed() {
  16. #failed is the number of students with grades below 50
  17. failed=`awk -F":" '$3<50' $file | awk '{print NR}' | tail -1`
  18. echo $failed
  19. }
  20.  
  21. #shows all the students with the highest marks
  22. Top() {
  23. top=`awk -F":" '{ print $3 }' $file | sort -nr | head -1`
  24. echo -e "`awk -F":" '($3 == top) { print $2 }' top=$top $1`"
  25. }
  26.  
  27. #outputs the average mark
  28. Avg(){
  29. total=0
  30. count=0
  31. mark=0
  32. while read line
  33. do
  34. #sets current mark the loop is looking at
  35. $mark=`echo $line | awk -F":" '{ print $3 }' $file`
  36. #echo $mark
  37. #adds mark to the total
  38. $total=`echo $(($total+$mark)) | bc`
  39. #echo $total
  40. #increases counter
  41. $count=$(($count+1))
  42. done <$file
  43. echo "scale=2; $total/$count" | bc
  44.  
  45. }
  46.  
  47. #make sure the input file exists
  48. if [ ! -f $file ]
  49. then
  50. echo "Input file [$file] not found - Aborting"
  51. exit
  52. fi
  53. if [ "$1" == m ]
  54. then
  55. topAndBotGrade $file
  56. elif [ "$1" == f ]
  57. then
  58. Failed $file
  59. elif [ "$1" == n ]
  60. then
  61. Top $file
  62. elif [ "$1" == a ]
  63. then
  64. Avg $file
  65. else
  66. #error trapping.
  67. echo "Invalid input."
  68. fi
Add Comment
Please, Sign In to add comment