vendredi 29 mars 2019

If statement test fails when comparing numbers read from file

I have a file with the following input (Sample not full file) JANE,SMITH,12,1000298,2 CLARA,OSWALD,10,10890298,4 We have FirstName, Lastname, Grade, ID, and School. I have a loop that reads each into their own variable. The last number (2,4) indicates what school they belong to and I have the code that changes the 2 to HS, and 4 to ML. I need to have the test pass. Where if it finds 2 do this find a 3 do this and so on.

#!bin/bash
OLDIFS=$IFS
IFS=","

while read  First_Name Last_Name Grade Student_Id school
do 


 if [[ $school == 2 ]]
    then
        School=$(echo $school | sed -e 's/2/HS/g')
    elif [[ $school == 3 ]]
     then
        School=$(echo $school | sed -e 's/3/MI/g')
      else
       School=$(echo $school | sed -e 's/4/ML/g')
   fi
   echo $First_Name $Last_Name $Grade $Student_Id $School
done < $1
IFS=$OLDIFS

Ok. So school has 2,4 as per the input from the file. When it finds a 2 it should change that 2 to HS. But the test fails. Even if I use -eq it fails. I add "" just to see if it did anything, but nothing. When I echo $school it gives me the right numbers 2,4 but It fails to compare it. Correct output JANE,SMITH 12 1000298 HS CLARA OSWALD 10 10890298 ML

What I get is CLARA OSWALD 10 10890298 ML As it skips straight to the else part. It does not check the first one. And if I try to check for $school == 4 or (-eq) it will just fail that too.

Aucun commentaire:

Enregistrer un commentaire