I started bash scripting only recently for university.
I have been trying to learn loops, and I have the following:
Guessing_Game() {
echo "Guessing Game!"
set loopSwitch = 0
until [[ $loopSwitch == 1 ]];
echo $loopSwitch
do
read -p "Guess a number between 1 and 10: " userGuess
if [[ $userGuess == 6 ]];
then
echo "Well done! You win."
else
echo "Incorrect guess."
fi
read -p "Would you like to play again? (y/n)" playAgain
if [[ $playAgain == "n" ]];
then
set loopSwitch = 1
fi
done
}
From what I have read, as the loopSwitch variable was set outside of the subshell, it can only change the value within that subshell?
How can I go about setting the loopSwitch variable so that I can exit the loop?
I have tried echoing out the variable $loopSwitch through several portions of the code - just after the entry into the until loop, inside the last if statement - and nothing is output, so I'm thinking it just doesn't exist within the until loop?
For the
set loopSwitch = 1
portion, I've tried:
set loopSwitch = 1
set $loopSwitch = 1
loopSwitch = 1
$loopSwitch = 1
I've looked online, but I just can't find any answers. I know it's simple, but I'm new.
Any help would be appreciated, whether that be with the code or constructs as a whole.
Thanks.