0

I am working on this code where I redirected a set of 60 numbers into an array and I was wondering if there was a way to get the if statement part to run for all 60 numbers without having to copy and paste the set of if statements and change the embassy value 60 times?

Revision: So to clear up my question here is my is my code and I want to run each number I saved into the array to run through the while loop without having to create an if statement block for each embassy value.

readarray -t score < hw4spr21grades.txt 
echo "${score[*]}" 
#echo "" "${#score[@]}"
declare y=0
declare A=0
declare B=0
declare C=0
declare D=0
declare F=0
while [ "$y" -le 60 ] 
do
if [ "${score[0]}" -le 100 ] && [ "${score[0]}" -ge 90 ]
then
    echo "${score[0]}" : 'A' >> hw4spr21output.txt
    ((y++))
    ((A++))
elif [ "${score[0]}" -le 89 ] && [ "${score[0]}" -ge 80 ]
then
    echo "${score[0]}" : 'B' >> hw4spr21output.txt 
    ((y++))
    ((B++))
elif [ "${score[0]}" -le 79 ] && [ "${score[0]}" -ge 70 ]
then
    echo "${score[0]}" : 'C' >> hw4spr21output.txt 
    ((y++))
    ((C++))
elif [ "${score[0]}" -le 69 ] && [ "${score[0]}" -ge 60 ]
then
    echo "${score[0]}" : 'D' >> hw4spr21output.txt 
    ((y++))
    ((D++))
elif [ "${score[0]}" -le 59 ] && [ "${score[0]}" -ge 0 ]
then
    echo "${score[0]}" : 'F' >> hw4spr21output.txt 
    ((y++))
    ((F++))
fi

if [ "${score[1]}" -le 100 ] && [ "${score[1]}" -ge 90 ]
then
    echo "${score[1]}" : 'A' >> hw4spr21output.txt
    ((y++))
    ((A++))
elif [ "${score[1]}" -le 89 ] && [ "${score[1]}" -ge 80 ]
then
    echo "${score[1]}" : 'B' >> hw4spr21output.txt 
    ((y++))
    ((B++))
elif [ "${score[1]}" -le 79 ] && [ "${score[1]}" -ge 70 ]
then
    echo "${score[1]}" : 'C' >> hw4spr21output.txt 
    ((y++))
    ((C++))
elif [ "${score[1]}" -le 69 ] && [ "${score[1]}" -ge 60 ]
then
    echo "${score[1]}" : 'D' >> hw4spr21output.txt 
    ((y++))
    ((D++))
elif [ "${score[1]}" -le 59 ] && [ "${score[1]}" -ge 0 ]
then
    echo "${score[1]}" : 'F' >> hw4spr21output.txt 
    ((y++))
    ((F++))
fi

if [ "${score[2]}" -le 100 ] && [ "${score[2]}" -ge 90 ]
then
    echo "${score[2]}" : 'A' >> hw4spr21output.txt
    ((y++))
    ((A++))
elif [ "${score[2]}" -le 89 ] && [ "${score[2]}" -ge 80 ]
then
    echo "${score[2]}" : 'B' >> hw4spr21output.txt 
    ((y++))
    ((B++))
elif [ "${score[2]}" -le 79 ] && [ "${score[2]}" -ge 70 ]
then
    echo "${score[2]}" : 'C' >> hw4spr21output.txt 
    ((y++))
    ((C++))
elif [ "${score[2]}" -le 69 ] && [ "${score[2]}" -ge 60 ]
then
    echo "${score[2]}" : 'D' >> hw4spr21output.txt 
    ((y++))
    ((D++))
elif [ "${score[2]}" -le 59 ] && [ "${score[2]}" -ge 0 ]
then
    echo "${score[2]}" : 'F' >> hw4spr21output.txt 
    ((y++))
    ((F++))
fi
done
Jesse Hix
  • 83
  • 5
  • Does [this](https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash) answer your question? It's about an array of strings, but the same syntax works for an array of integers (actually, bash doesn't really distinguish them; as far as bash is concerned, numbers are just strings that happen to be all digits). – Gordon Davisson Feb 10 '21 at 06:47

3 Answers3

0

try to use for/while loop method while running only one if statement

0

Here is where I found a solution. Loop through an array of strings in Bash?

declare y=1
while [ "$y" -le 60 ] 
do
if [ "${score[$y-1]}" -le 100 ] && [ "${score[$y-1]}" -ge 90 ]
then
    echo "${score[$y-1]}" : 'A' >> hw4spr21output.txt
    ((y++))
    ((A++))
Jesse Hix
  • 83
  • 5
-1

Example:

let bobsFollowers = ['Joe', 'Marta', 'Sam', 'Erin'];
let tinasFollowers = ['Sam', 'Marta', 'Elle'];
let mutualFollowers = [];

for (let i = 0; i < bobsFollowers.length; i++) {
  for (let j = 0; j < tinasFollowers.length; j++) {
    if (bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers.push(bobsFollowers[i]);
    }
  }
};