jeudi 23 septembre 2021

Can I put an IF statement inside of a FOR loop using BASH?

Hello I am coding in Bash. I want to create a for-loop that prints the {classId} if "isliveEnabled" is false. However instead of just printing the classId with a "false" indication, it prints the whole list. Below is my code and output. Please let me know what I am doing wrong. Thank You!

Here are the values in ${classes}

classId: 75000
isliveEnabled: true
timezoneName: "America/New_York"
classId: 85000
isliveEnabled: false
timezoneName: "America/Chicago"
classId: 95000
isliveEnabled: false
timezoneName: "America/Chicago"
classId: 99000
isliveEnabled: false
timezoneName: "America/Detroit"
classId: 100000
isliveEnabled: false
timezoneName: "America/Chicago"

Code:

for class in ${classes}
do
   if [[ ${isliveEnabled} -eq 'false' ]]
   then

      IFS=', ' read -r -a array <<< "$class"
      echo "    classId: ${array[0]}" 

   fi
done

Output:

classId: 75000
classId: 85000
classId: 95000
classId: 99000
classId: 100000

Aucun commentaire:

Enregistrer un commentaire