Learn Bash Scripting by Building Five Programs - Build Five Programs

Tell us what’s happening:

I am stuck in this level: You used the double square brackets with your if statement in the last program, but you can use the double parenthesis with these operators as well. In your script, create an if statement that uses double parenthesis for the condition. Check if the number variable is less than or equal to 15. If it is, use your two variables to print The next number is, B:<number> .

Can you please tell me what is wrong with my code?

Your code so far

#!/bin/bash

Bingo Number Generator

echo -e “\n~~ Bingo Number Generator ~~\n”

NUMBER=$(( RANDOM % 75 + 1 ))

TEXT="The next number is, "

if (( NUMBER <= 15 ))

then

echo $TEXT B:$NUMBER

fi

echo $NUMBER

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Bash Scripting by Building Five Programs - Build Five Programs

The shebang line is used to specify the interpreter for the script, and it should be placed at the very beginning of your script. The correct shebang line for a Bash script is #!/bin/bash. Also, there’s an issue with the double quotes used around the echo statement.

Here’s the corrected version of your script:

#!/bin/bash

# Bingo Number Generator
echo -e "\n~~ Bingo Number Generator ~~\n"

NUMBER=$(( RANDOM % 75 + 1 ))

TEXT="The next number is, "

if (( NUMBER <= 15 )); then
    echo "$TEXT B:$NUMBER"
fi

echo $NUMBER

Changes made:

  • Added the correct shebang line.
  • Corrected the double quotes around the echo statement.

With these changes, your script should work as intended. It generates a random number between 1 and 75, checks if it’s less than or equal to 15, and if so, prints the message with the number. Then, it prints the generated number regardless of the condition.

Thanks for your reply.
I tried the code you provided but it still not working.
Here is my code now :

#!/bin/bash

# Bingo Number Generator
echo -e "\n~~ Bingo Number Generator ~~\n"

NUMBER=$(( RANDOM % 75 + 1 ))

TEXT="The next number is, "

if (( NUMBER <= 15 )); then
    echo "$TEXT B:$NUMBER"
fi

echo $NUMBER

I want to mention that in the code i provided first:

#!/bin/bash

# Bingo Number Generator
echo -e "\n~~ Bingo Number Generator ~~\n"

NUMBER=$(( RANDOM % 75 + 1 ))

TEXT="The next number is, "

if (( NUMBER <= 15 )); then
    echo $TEXT B:$NUMBER
fi

echo $NUMBER

This script worked in the terminal but yet it didnt pass the level and i keep getting this error : Your script should have the suggested “if” statement added correctly

#!/bin/bash

# Bingo Number Generator
echo -e "\n~~ Bingo Number Generator ~~\n"

NUMBER=$(( RANDOM % 75 + 1 ))

TEXT="The next number is, "

if [[ $NUMBER -le 15 ]]; then
    echo "$TEXT B:$NUMBER"
fi

echo $NUMBER

In this version, I’ve replaced the double parentheses with double square brackets and used the -le operator for “less than or equal to”. This is another valid way to express the condition.

If this still doesn’t resolve the issue on the platform, I recommend checking the specific requirements or instructions provided by the platform for the correct syntax they expect. The platform might have a specific way of validating the “if” statement that differs from standard Bash scripting.