until:-
This construct tests for a condition at the top of a loop, and keeps looping as long as that condition is
false (opposite of while loop).
until [ condition-is-true ]
do
command(s)...
done
Note that an until loop tests for the terminating condition at the top of the loop, differing from a
similar construct in some programming languages.
As is the case with for loops, placing the do on the same line as the condition test requires a
semicolon.
until [ condition-is-true ] ; do
Example 10-18. until loop
#!/bin/bash
END_CONDITION=end
until [ "$var1" = "$END_CONDITION" ]
# Tests condition here, at top of loop.
do
echo "Input variable #1 "
echo "($END_CONDITION to exit)"
read var1
echo "variable #1 = $var1"
echo
done
exit 0
---------- Post updated at 04:36 AM ---------- Previous update was at 04:27 AM ----------
if you understand the above you have to change your script to the below so that the until loop does not terminate:-
#!/bin/bash
clear;
op = 0
until [ $op -ne 0 ]
do
echo "Select from menu:"
echo "1. Print Text"
echo "2. Exit"
read q
case $q in
1) echo "Hello World!!!";;
2) exit;;
esac
done
Indeed, since the OP's statement would not work. It would work with a while statement. The code to exit is 2 according to his menu. So a while loop would loop while the condition is not equal to 2, whereas an until loop loops until the condition equals 2.
it's a weird loop.
with -ne : it will never enter the loop because 0 -ne 0 returns FALSE
with -eq : it will loop infinitely because 0 -eq 0 always returns TRUE and that $op is never modified.
It really does not matter what you put there as long as it evaluates to false.
Only now I noticed the OP was also mixing up q and op :o. Besides. If 2 selects an exit statement then Oh well..
But your code still would not work because of the erroneous assigment: