Need Help for "until" construction

Hello Unix guru's !!!

I have problem with this simple script:

#!/bin/bash
clear;
op = 0
until [ $op -ne 2 ]
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

The result is:

./file: line 4: [: -ne: unary operator expected
Select from menu:
1. Print Text
2. Exit

How can I get rid of this message "/file: line 4: [: -ne: unary operator expected"?

modify the code to below

until [ "${op}" -ne 2 ]
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

It's send me new error:

#!/bin/bash
until [ "${op}" -ne 2 ]
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

The result:

./file: line 2: [: : integer expression expected
Select from menu:
1. Print Text
2. Exit

The problem is in the assignment of "op": you should not use spaces there. Also the condition should be the other way around.

#!/bin/bash
clear
op=0
until [ $op -eq 2 ]
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

until syntax & help...kindly read this.

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

That does not seem right. It still contains the wrong assignment statement and it will exit on e.g. value 3. Check my post above.

in your post you are using "-eq" but in niki22 post he is using "-ne" ..you have invert the condition ...so it will give you true.

BR

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.

That's right, that is why I used this:

until [ $op -eq 2 ]

It seems pretty straight forward to me...

I hope this loop is clear to you now guys... :slight_smile:
BR

Indeed, it is like

until false; do
done

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:

op = 0

which should be:

op=0

yes you are right:- where niki22 code will not work unless he doing op=0 not op = 0

BR