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

1 Like

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…

2 Likes

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.

3 Likes

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!

1 Like

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

1 Like

Here’s my nested loop, for anyone struggling to proceed. Note that for the first while loop condition, the code passed with -lt 3 but I changed it to -le 3 because the expected result was “Nice to meet you!” followed by two “How are you?” greetings. I think this is a mistake on their end, so you’re fine using -lt … it just bugged me that I wasn’t seeing the stated result.

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

Agree with other criticisms of this lesson and the Bash scripting module in general. The pace suits me—after taking JavaScript and Ruby I understand the concepts (more or less) and can simply apply this syntax—but how’s a beginner supposed to comprehend incrementing or what “1 indexed” means?! I recommend starting with JavaScript fundamentals (can’t speak to Python) because those early lessons do a much more thorough job breaking down concepts. It doesn’t look like the Bash course was designed to do so.

Thx Brother It Helped Me, Was Just Messing With Condition From 20mins

1 Like

Have to add my voice to the others who are critical of this section of the Command Line course. Like many others here, I am a complete beginner to programming. I only found my way to the Command Line course because I was about to begin Python 3, and it was recommended to have an understanding of Command Line before getting started. So I followed the link to the Command Line course, thinking that was the right logical progression. And for the first half of the course, I did find find it rather straight-forward and intuitive.

Unfortunately, the course appears to have fallen apart in the second half (most notably here in the Bash Scripting section). Instructions are unclear, and written with the assumption that we have a lot of knowledge that has not yet been provided. Been trying to wrestle my way through the Loops + Inputs pages on this course, but after seeing all of the other voices here agreeing that it’s not well made, I think I’ll find a lesson on this elsewhere.

1 Like

:sob: :sob:Thanks for this!!

1 Like

I wanted to state the amount of information in description combined with a bit blurry instructions isn’t put in a beginner friendly way,

I have started CLI course as it was recommend prior to starting Python & going off platform with SQL.

I have struggled with the “Loops” lesson - then checked the ‘‘view solution’’ option as advised while seeking help here - got frustrated as it didn’t differ much from what I wrote - took a break - restarted progress - tried to write it manually to get the concept again - - - and I admit despite getting it done within the first try I feel just as confused as at the beginning…

I guess the only way is through :sweat_smile:

Best regards & don’t give up

Not the greatest lesson, unfortunately. Thanks to everyone in this thread for the guidance. In the end it was the index=$((index + 1)) that threw me off. There is no ‘index’ variable in this exercise, but rather a ‘greeting_occasion’ variable.

The instruction on this topic was not clear in terms of guidance on the use of loops