I have a task to do and I'm struggling with it:
Write a BASH script that reads student names and grades from a file into an array. Prompt the user for a student name. Using the binary search method, find the name in the array and display the grade. If the student does not exist, print a message stating that.
And here is the data in the file:
Ann:A
Bob:C
Cindy:B
Dean:F
Emily:A
Frank:C
Ginger:D
Hal:B
Ivy:A
Justin:F
Karen:D
So, what I did first was to create a file in my Linux OS called "students" in my home directory with the structure above. Then in my BASH script I created the following script:
#!/bin/bash
#Store from a file called "students" the first field before " : " into array
names=( $( cut -d: -f1 students ) )
#Store from a file called "students" the second field after " : " into array
grades=( $( cut -d: -f2 students ) )
echo "Enter the Student Name:"
read inputname
for i in "${names[@]}"; do
echo $i;
done
for j in "${grades[@]}"; do
echo $j;;
done
With that information I can now see when each array is "co-related" (like for example in the "names array" we have Ann, in the "grades array" we have her score of "A", and so on)
My problem is how can I "link" these two informations? When for example someone inputs Bob, it will automatically output "Bobs grade is C" or stuffs like that? Is it possible to "link" two array values (like in the namesarray[0] equivalent to gradesarray[0], namesarray[1] equivalent to gradearray[1] and so on) in BASH ? Or am I doing something wrong here that I'm missing?
Thank you for your help!