1

I have a shell script as follows (taken from some lecture slides):

#/bin/sh
echo -e "enter a number:\c"
read number
if [$number -ne 2]
  then
     echo "Number is not equals to 2"
fi

And I'm getting a syntax error where fi is. Any idea what the problem is?

Also, what does the extra term in echo -e "enter a number:\c" means (asides from the simple fact that it asks for a number)?

EDIT: now I did

#/bin/sh
echo -e "enter a number:\c"
read number
if [ "$number" -ne 2 ]
then
   echo "Number is not equals to 2"
fi

And I'm still getting the error...

Same goes for

#/bin/sh
read -p "enter a number: " number
if [ "$number" -ne 2 ]
then
   echo "Number is not equals to 2"
fi 

SOLVED: I've made a copying error there. Thanks for the input by the way, guys.

7
  • 1
    Your revised script (immediately following "EDIT: now I did) works correctly; when I run it and enter 2 it doesn't print the message. Commented May 7, 2014 at 21:05
  • Ok, I think I realised my little mistake and fixed it. Commented May 7, 2014 at 21:06
  • 1
    It works for me. Is the script in your question exactly the same as what you're running? Did you copy-and-paste it into your question? Commented May 7, 2014 at 21:10
  • 1
    Step 3 in the bash tag wiki is "Test your example. Make sure it runs and still shows the problem. Do not brush this off." :P Commented May 7, 2014 at 21:18
  • 1
    @thatotherguy Sorry, newbie here :/ and I was kind of in a rush... You know, we have a tendency to rush things. But I did test the code and it was actually just a minor copying code in another part of the file... My bad. Commented May 7, 2014 at 21:20

1 Answer 1

2

Problem is this if condition:

if [$number -ne 2]

You need to put space after [ and before ] so use:

if [ "$number" -ne 2 ]

Your script can be rewritten as:

#/bin/sh
read -p "enter a number: " number
if [ "$number" -ne 2 ]
then
   echo "Number is not equals to 2"
fi

However if bash is available then better to switch to bash instead of old bourne shell.

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

5 Comments

ok replace #/bin/sh by #/bin/bash and run it using bash -ex script.sh
@user3511965: A $ doesn't tell you what shell you're running. If you're running bash, then echo $BASH_VERSION will show its version number. But the shell you're running interactively is not related to the shell used to run your script. The suggestion is to change #!/bin/sh to #!/bin/bash in your script -- though in this case it doesn't make much difference.
@anubhava: What's the advantage of bash over sh for this script?
Okay, thanks! It's working now, just a little error of mines. What about the -e tag and \c, what are those?
@KeithThompson No real advantage, I wrote answer using /bin/sh itself but OP still ran into issues so I suggested bash just for a testing since I am not sure why is /bin/sh not working for OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.