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.

A J
  • 3,970
  • 14
  • 38
  • 53
Akshita
  • 49
  • 5

2 Answers2

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
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.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318