LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash is not exiting the while loop. (https://www.linuxquestions.org/questions/linux-newbie-8/bash-is-not-exiting-the-while-loop-4175615838/)

JoseKreif 10-17-2017 01:11 PM

Bash is not exiting the while loop.
 
Maybe I'm having a bad day, or maybe this is another reason for why I hate writing bash.

Code:

#!/bin/bash

xvloop='x'
xchoice=''
XSTATUS="N/A"

while [ "$xvloop" = 'x' ]; do
    clear

        printf  "\n\n
Some Status: ${XSTATUS}

                Some Sub Menu

                A: Some Function
                B: Go Back

        Choice: "

        read choice

        case $xchoice in
          [Aa]) echo "Hello World"; XSTATUS="Changed"; clear;;
          [Bb]) xvloop='Z'; xchoice=""; clear; break;;
    esac

# DEBUG MESSAGE
echo "${xvloop} - ${xchoice}"

done

# DEBUG MESSAGE
echo "PAST DONE"

I am stuck in the while loop. When I print out the xvloop variable, it's still set to x... While xchoice is "" (blank).

EDIT: D'OH! I am reading a null variable.

ShadowCat8 10-17-2017 07:45 PM

Greetings,

Well, I would say that the issue with this code has nothing to do with which scripting language you used... You would likely still have an issue for the small error (probably typo) that is there whether it was written in BASH, csh, ksh or python... So, you were probably just having a bad day.

On line 20, I would recommend changing "read choice" to "read xchoice" so you are actually taking the input to the variable you are checking against in the case switch.

HTH your day get better. Let us know.


All times are GMT -5. The time now is 12:19 AM.