0

I'm trying to read from user and then do the following.

read framechoice
 if [ $framechoice -gt 100 ]; then 
   if [ $framechoice -lt 0 ]; then
          framechoice=101
   fi
 fi

It gives me the following error.

[: -gt: unary operator expected

Can anyone tell me where I am going wrong.

2 Answers 2

1

This happens if you don't input anything:

$ cat myscript
read framechoice
 if [ $framechoice -gt 100 ]; then
   if [ $framechoice -lt 0 ]; then
          framechoice=101
   fi
 fi
$ bash myscript
<enter>
myscript: line 2: [: -gt: unary operator expected

Try instead to actually enter something:

$ bash myscript
42<enter>

The script then exits with success.

Sign up to request clarification or add additional context in comments.

Comments

1

Your program needs to cope with empty input. This is most easily achieved by properly quoting the variable; then

if [ "$framechoice" -gt 100 ]; then

evaluates to [ "" -gt 100 ] which is no longer is a syntax error; however, instead, it throws the warning integer expression expected.

Even better, maybe filter the input so that you do not attempt numeric comparisons before making sure the input is numeric.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.