Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created August 1, 2013 07:21
Show Gist options
  • Save jsfaint/6129154 to your computer and use it in GitHub Desktop.
Save jsfaint/6129154 to your computer and use it in GitHub Desktop.
An bash script resolve the topic described in "Question.txt"
#!/bin/bash
rec_dir=$1
output="students.gpa"
if [[ $# -ne 1 ]]; then
echo "please input the record directory."
exit 1
fi
student_id_list=`sed -n "/^[1-9]/"p $rec_dir/* | awk '{print $1}' | sort -u`
course_cnt=`ls -l $rec_dir/* | grep ^- | wc -l`
student_cnt=`echo $student_id_list | awk '{print NF}'`
#caculate score
function calc_score()
{
local student=$1
local course=$2
local sc=0
local course_credits=`sed -n "2p" $rec_dir/$course | awk '{print $2}'`
credits[$cnt]=$course_credits
grep -w $student $rec_dir/$course > /dev/null
if [ $? -ne 0 ]; then
sc="-"
else
#calculate score here :)
local raw=`grep -w $student $rec_dir/$course | awk '{$1=0; print $0}'` #remove student id
for i in $raw;
do
sc=`expr $sc + $i`
done
fi
score[$cnt]=$sc
}
#caculate GPA
function calc_gpa()
{
local cnt=$1
local raw_header="scale=2;("
local raw_mid="*4)/("
local raw_tail="*100)"
local num=0
local deno=0
local cnt_base=`expr $cnt \* $course_cnt`
for (( co = 0; co < $course_cnt; co++ )); do
local s=${score[$cnt_base + $co]}
if [[ $s == "-" ]]; then
continue
fi
num=`expr $num + $s \* ${credits[$i]}`
deno=`expr $deno + ${credits[$i]}`
done
gpa[$cnt]=`echo "$raw_header$num$raw_mid$deno$raw_tail" | bc`
}
cnt=0
for st in $student_id_list;
do
for co in `ls $rec_dir`;
do
calc_score $st $co
cnt=`expr $cnt + 1`
done
done
for (( i = 0; i < $student_cnt; i++ )); do
calc_gpa $i #i means student count
done
cnt=0
for st in $student_id_list;
do
student[$cnt]=$st
cnt=`expr $cnt + 1`
done
#Output start here
#title
echo -n "Student#" > $output #new file
for co in `ls $rec_dir`;
do
course_name=`sed -n "1p" $rec_dir/$co | awk -F ":" '{print $2}' | sed "s/^\s//"`
echo -ne " $course_name" >> $output
done
echo -ne " GPA\n" >> $output
#student, score and GPA
for (( st = 0; st < $student_cnt; st++ )); do
echo -ne "${student[$st]}\t" >> $output
cnt_base=`expr $st \* $course_cnt`
for (( co = 0; co < $course_cnt; co++ )); do
echo -ne "${score[$cnt_base + $co]}\t" >> $output
done
echo -ne "${gpa[$st]}\n" >> $output
done
Description
Assume there is a directory contains student records regarding to different courses. These files have “.rec” extension. Each file belongs to a course and has the following format:
COURSE NAME: <the name of the course>
CREDITS: <credits>
Student# <a list of numbers belong to different activities have been done in the course>
For example a course record file can be like this:
COURSE NAME: Operating Systems
CREDITS: 4
123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25
243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15
Your job is to create a file in the current directory which contains the list of all students, their total marks for each course, and their GPA as the last column. More specifically the file should be named “students.gpa” and has the following format:
Student# <course name> <course name> <course name> …
For example a GPA file could be like this:
Student# Operating Systems JAVA C++ Web Programming GPA
123456 76 63 50 82 67.75
243567 80 - 34 63 59
Note that it is possible that all students don’t have all courses. For those courses in the GPA file you should put ‘-‘ instead of the mark for the student mark.
Notes
1. Name your script “gpacalc.s”.
2. The name of the directory which contains all record files should be passed into your script. For example: gpacalc.s ~/2011/Fall/StudentsRecord
3. The output file should be named “students.gpa” and be stored in the input directory. If the file exists overwrite it.
4. You should check the input directory is a valid directory in the beginning of your script. If it is not a valid directory print an appropriate message and exit.
5. Students in the file should be sorted in non-descending order (from small number to big numbers).
6. If you create intermediate files anywhere including in /tmp, before the end of the script remove them.
7. There may be other files in the input directory which are not record files. You should only consider those files which contain student marks.
8. Test your script before submit it.
9. IF YOUR SCRIPT HAS SYNTAX ERROR, YOU GET ZERO FOR THIS ASSIGNMENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment