FAQ: Learn Bash Scripting - Loops

Hi,

What is wrong in my code?

#!/bin/bash
first_greeting=“Nice to meet you!”
later_greeting=“How are you?”
greeting_occasion=0
while [ $greeting_occasion -lt 3 ]
do
if [ $greeting_occasion -lt 1 ]
then
echo first_greeting greeting_occasion=((greeting_occasion + 1))
else
echo later_greeting greeting_occasion=((greeting_occasion + 1))
fi
done

What is wrong?

1 Like

This loops lesson is very badly written, its wasted my time just to find that this lesson is badly written

Absolutely the worst lesson I’ve encountered so far on Codecademy. Stuck and it won’t let me progress, no guidance given. Horrible.

jfc 43 comments and nobody has yet explained how they made this work. Can somebody just tell me what code they used that allowed them to advance, since Codecademy itself refuses to teach it to me?

Try running into this the DAY after your membership for an entire year got billed to your account.

1 Like

After giving up and looking at the result, I am confused. I definitely had the right code at some time, but it didn’t work. I don’t know where I messed up.

Hi all,

While doing it it felt a bit unclear also. After 30 mins of analysing the syntax I got it sorted.

Code below for solution

while [ $greeting_occasion -lt 3 ]
do
if [ $greeting_occasion -lt 1 ]
then
echo $first_greeting
else
echo later_greeting fi greeting_occasion=((greeting_occasion + 1))
done

So the “if” function takes place of the “echo index" part of the "while" loop and the loop finishes with the "index=((index + 1))”.

I think the confusion here is that the error msg shows to set the if…fi between the do and done when in reality there is still the “index=$((index +1))” missing co complete the loop.

Give it a try see if it works out. Hopefully I’m clear enough to help as I’m also starting out…

1 Like

I had problems with this exercise, but in the end the problem was occasion != ocassion. :wink:
But also, the red underscore, and red last bracket were confusing, as I thought there’s an error in my code!
image
Basically it’s:

while [condition]
do
....if[condition]
........// code
....then
........// code
....fi
....increment
done

Hope it’s helpful.

Did no one proof read these lessons???
Please highlight the ‘for’ within this lesson!
These lessons are already horribly written, it doesn’t help that the formatting is wrong too.

trueeee!
I can’t progress in this course afterbthis lesson.

Hi. So, if you click the “View Solution” button (next to a lightbulb) at the lower right of your code editor window, it will provide the code text it expects and allow you to progress. This button will only appear after you have tried and failed twice.

I had never had to use it prior to this lesson, so I discovered it only after my post (where I was admittedly very frustrated).

My criticisms of this particular lesson still stand, though. I have been quite happy with some of the others.

Good luck!

I experienced the same problem. Didn’t notice the space inside the brackets until I looked at the solution. I tested this same script in my terminal app on my Mac. The spaces ARE required! Bash complained about running the same script until I inserted the spaces inside the brackets.

I think the codecademy lesson writer could have done a better job pointing out the necessity of those spaces.

image