Possible Syntax Error - Bash Scripting Basics - Guided Exercise

Part 3: Reusable Scripts of 18.

#!/bin/bash

#Create the fruits array from the fruits text file
mapfile -t fruits < “fruits.txt”

#Assign variables based on the fruits array’s length
arrayLength=${#fruits[@]}

firstFruit=0
lastFruit=$((arrayLength-1))

#Print array facts to the screen

echo “There are $arrayLength fruits in the array.”
echo “The first fruit is ${fruits[$firstFruit]}.”
echo “The last fruit is ${fruits[$lastFruit]}.”

#Compare every fruit in the fruits array to “melon”
for $fruit in ${fruits[@]}
do
if [ $fruit != “melon” ]
then
echo “I love $fruit!”
else
echo “Yuck, $fruit.”
fi
done

for $fruit in ${fruits[@]} it should be for fruit in ${fruits[@]}

and melons instead of melon
if goal is to only add comments without chaning the code.

Thank you so much for pointing this out!!! :heart: :heart: :heart:

I have fixed the fruit and melons :slight_smile:

1 Like