Welcome to the Linux Foundation Forum!

Fixing The Variable's Content on a IF Clause - Shell Script

milton.neto
milton.neto Posts: 2
edited March 10 in LFS203 Class Forum

On Lesson 08 – Bash Shell Scripting – Testing for Strings(https://trainingportal.linuxfoundation.org/learn/course/linux-for-cloud-technicians-essentials-lfs203/bash-shell-scripting/bash-shell-scripting?page=22), the script somescript.sh contains an if clause that is not properly reading the content of the variable RESPONSE. Because of this, it always prints the same message:

The answer was not yes!

even when you type YES.

Below is the corrected version, somescript-1.1.sh:

#!/bin/bash 
echo "Enter yes or no:" 
read RESPONSE 
if [ "$RESPONSE" == "yes" ]; then 
echo "The answer was yes!" 
else 
echo "The answer was not yes!" 
fi 

The key fix is using "$RESPONSE" inside the test condition, so that the shell evaluates the value stored in the variable, not just its name as a string, and the script now behaves correctly.

Comments

  • fcioanca
    fcioanca Posts: 2,463

    Hi @milton.neto

    Thank you for flagging this issue. It has now been corrected in the course.

    Best regards,
    Flavia

Categories

Upcoming Training