Sunteți pe pagina 1din 4

1.

Script cu un catalog în care poți adăuga studenți, notele lor și calcularea


mediei unui student.

#!/bin/bash

declare -A student_grades
declare -A student_grades_number
max_students=10
id=0

addStudent(){
while true; do

echo
read -p "Nume si prenume student:" nume
read -p "Grupa:" group
read -p "Anul Studentesc:" an
data="Id student:"
data+=$id
data+=" Nume Student:"
data+=$nume
data+=" Grupa:"
data+=$group
data+=" Anul de studii:"
data+=$an
((id++))
echo
read -p "Ati introdus informatiile corecte?(y\n):" da_nu
da_nu=${da_nu:-y}
if [ $da_nu != Y ] && [ $da_nu != y ]; then
return
fi
echo -e "\nAm adugat studentul in baza de date"
echo $data >> "students"
read -p "Mai aveti studenti de adaugat?(y/n):" da_nu
da_nu=${da_nu:-y}
if [ ! $da_nu = Y ] && [ ! $da_nu = y ]; then
return
fi

done

gradeStudent(){
while true; do

echo
read -p "Numele studentului care va fi notat:" nume
read -p "Nota: " nota

read -p "Ati introdus informatiile corecte?(y\n):" da_nu


da_nu=${da_nu:-y}
if [ $da_nu != Y ] && [ $da_nu != y ]; then
return
fi
data=$(grep "$nume" "students")
touch "tmp_file"
echo $data>>"tmp_file"
student_id=$(cut -d " " -f 2 students | cut -d ":" -f 2)
rm tmp_file
student_last_grade=${student_grades_number[`expr $student_id `]}
index=$((student_id*10+student_last_grade))
student_grades[$index]=$nota
((student_grades_number[$student_id]++))

for((j=0;j<=student_last_grade;j++))do
index=$((student_id*10+j))
echo ${student_grades[$index]}
done

read -p "Mai aveti studenti de notat?(y/n):" da_nu


da_nu=${da_nu:-y}
if [ ! $da_nu = Y ] && [ ! $da_nu = y ]; then
return
fi

done
}
computeGrade(){
while true; do

echo
read -p "Numele studentului caruia vrei sa-i calculezi media:" nume
read -p "Ati introdus informatiile corecte?(y\n):" da_nu
da_nu=${da_nu:-y}

if [ $da_nu != Y ] && [ $da_nu != y ]; then


return
fi

data=$(grep "$nume" "students")


touch "tmp_file"
echo $data>>"tmp_file"
rm tmp_file
student_id=$(cut -d " " -f 2 students | cut -d ":" -f 2)
student_last_grade=${student_grades_number[`expr $student_id `]}
sum=0

for((j=0;j<student_last_grade;j++))do
index=$((student_id*10+j))
sum=$((${student_grades[$index]} + $sum))
done

sum=$(echo "scale=9; $sum/$student_last_grade" | bc)


echo "Studentul $nume are media $sum."
data+=" Media:$sum"
echo $data >> "grades"

read -p "Mai aveti studenti carora vreti sa le calculati media?(y/n):" da_nu


da_nu=${da_nu:-y}
if [ ! $da_nu = Y ] && [ ! $da_nu = y ]; then
return
fi
done
}

show_menu()
{
clear
echo "++++++++++++ MENU +++++++++++++"
echo "1. Adauga un Student."
echo "2. Da o nota unui student."
echo "3. Calculeaza media unui student"
echo "4. Exit"
echo "+++++++++++++++++++++++++++++++"
}

take_input()
{

local choice
read -p "Selecteaza o optiune: " choice

case $choice in
1) addStudent ;;
2) gradeStudent ;;
3) computeGrade ;;
4) exit 0 ;;
*) echo "Ati introdus un input gresit !!"
read -p "Apasa pe orice buton pentru a continua..."

esac
}

packages=("bc")

for pkg in $packages; do


if dpkg --get-selections | grep -q "^$pkg[[:space:]]*install$" >/dev/null; then
echo -e "$pkg este deja instalat"
else
sudo apt-get install -y $pkg
echo "$pkg s-a intsalat cu succes"
fi
done

if [ -f "students" ];
then
echo
else
touch "students"
fi
if [ -f "grades" ];
then
echo
else
touch "grades"
fi
for((i=0;i<$max_students;i++)) do
student_grades_number[$i]=0
done

while true
do
show_menu
take_input
done

S-ar putea să vă placă și