mercredi 1 juillet 2020

How to use if else statements of different files' data/strings and output them together using Shell Script

The Problem: I have 3 .txt files with similar data and a method that I completed a method named course_list. I used a for loop to find the output which you can see below. Using that course_list method is NOT necessary but I am clueless of any other way.

The 3 .txt files are:

file1.txt

name :  Maria
age :  17

mat     1 0 1 0 1
che     1 1 1 0 1
his     0 1 1 1 0

file2.txt

name :  Matt
age :    17

mat     0 1 0 1 1
bus     1 1 1 1 1
geo     1 0 0 1 1
phy     0 1 1 0 1

file3.txt

name :  Rick
age :     18

mat       1 1 1 0 1
spo       0 0 1 1 0
com       1 1 1 1 1

These 3 txt files have a name, age, courses, and their grades (pretend its either 1 or 0). One of my methods manages to list the courses in sorted order with no duplicates.

Output of a method named course_list()

bus
che
com
geo
his 
mat
phy
spo

I managed to figure out how to list the names of the students in each file, age, and courses. However, I'm struggling to find anything else and use that data from finding the names and ages of those who took math class (just an example).

my expected output:

Students in mat class are :
1. Maria who is 17 years old
2. Matt who is 17 years old
3. Rick who is 18 years old

How would I use if statements in this case? or is there a better way to do so?

#user input 
read -p "Type the course name: course_name"

#if length of string is not equal to 3
if [ ${#course_name} -ne 3 ]; then
echo "name must be no longer or shorter than 3 characters." ;

#if course name entered is equal to one of the courses listed in course_list method, then return name and age of each student taking that course.
elif ["$course_name" ==  "$course_list"} then
....???

Aucun commentaire:

Enregistrer un commentaire