0

I'm making a bash script that asks the user for a number between 10 and 100 then gives echos that state whether the number was to low or to high.

Please excuse the code, I'm very new to bash scripting and still learning, I'm sure I have quite a bit of the basic Syntax errors.

Here is the code.

#!/bin/sh
echo "Enter a Number between 10 and 100."
    read num
if ["$num" -gt 10 -a "$num" -lt 100] ; then
    echo "The Number `$num` is just right"

elif ["$num" -lt 10] ; then 
    echo "Number to small"

elif ["$num" -gt 100] ; then
    echo "Number to big"

else 
    echo "Enter In a Number between 10 and 100"
fi

And Here is the error message that I have been getting.

$ ./Num*
./NumGuess.sh: line 7: [: missing `]'
./NumGuess.sh: line 10: [: missing `]'
./NumGuess.sh: line 12: [: missing `]'
Enter In a Number between 10 and 100

I have tried moving the echo "Enter a Number between 10 and 100." inside the while loop but that did not work. The loop is not looping back to the first if statement and is just shutting down after it takes the first user input and then producing errors.

YHapticY
  • 177
  • 11
  • 5
    Please take a look: http://www.shellcheck.net – Cyrus May 01 '19 at 21:16
  • 1
    Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww May 01 '19 at 21:44

1 Answers1

0

You code looks fine but the if command needs a space seperation. So please change

if ["$num" -gt 10 -a "$num" -lt 100] ; then

to

if [ "$num" -gt 10 -a "$num" -lt 100 ] ; then (And also in the other if statements).

The second thing is your using backticks in line 5 at echo "The Number $num is just right". You need to remove these. And you're good to go ;).

Enter a Number between 10 and 100.
3
Number to small
philipp@philipp-mbp ➜  stackoverflow ./test.sh
Enter a Number between 10 and 100.
23
./test.sh: line 5: 23: command not found
The Number  is just right
--> after removing backticks
stackoverflow ./test.sh   
Enter a Number between 10 and 100.
23
The Number 23 is just right
Buh13246
  • 173
  • 7
  • Thanks that fixed it up perfectly. How do I get the ```[ "$num" -lt 10 ]``` and the ```[ "$num" -gt 100 ]``` statements to run back through the first ```if``` statement? If it's not a quick fix never mind. Thanks a ton for the response, greatly appreciated! – YHapticY May 01 '19 at 22:19