0

Here is a random number generator followed by a line clear timer and you are then to try and input the number that you saw. I cant seem to set up the comparison between the randNum variable and the input. I tried setting a defined value for the input as well and still receive an error "command not found" when checking input vs randNum variable

n=$RANDOM$RANDOM$RANDOM; let "n %= 10000000000";
echo $n

for i in {5..1}; do echo $i; sleep 1; tput cuu1; tput el; done
tput cuu1; tput el

echo "what was that number?"
#read input
input=999999999
if [$input == $n]
then
echo "you are correct"
else
echo "you are incorrect"
fi

2 Answers2

0

Shells need spaces around brackets [ and ] when used for this purpose (replacement syntax for the test command), and you should better use -eq instead of == for numeric comparison:

if [ $input -eq $n ]
Jeff Y
  • 2,437
  • 1
  • 11
  • 18
  • wow, I literally ran -eq for 20 mins trying things to realize i didn't have spaces around my brackets.... Thank you – user3485895 Feb 27 '16 at 18:29
  • I would add that if "input" is read from stdin or somewhere else it should be enclosed in double quotes just to avoid run time errors. Like this: if [ "$input" -eq $n ] – Diego Augusto Molina Feb 27 '16 at 19:41
0

If you use the arithmetic comparison, rather than the old testcommand (a.k.a. [) then you can avoid these issues. Spaces here are less important inside the brackets:

if (( input == n ))

Note that the leading $ is not required (and using it can cause issues)

cdarke
  • 42,728
  • 8
  • 80
  • 84