1

I am new to shell scripting and this is my first shell script.i am getting this error and stuck in it.following is the simple code for it:

#!/bin/sh
yes=y;
no=n;
echo "Do you want to enter batch order id manually? (y/n) "
read answer
if [ $answer -eq $yes ]; then
    echo "Please Enter Batch Order Id."
elif[ $answer -eq $no ]; then
    echo "Copying all batch orders."
else
    echo"please enter correct input."
fi

1 Answer 1

3

The script needs a couple minor changes:

#!/bin/sh
yes=y;
no=n;
echo "Do you want to enter batch order id manually? (y/n) "
read answer
if [ "$answer" = $yes ]; then
    echo "Please Enter Batch Order Id."
elif [ "$answer" = $no ]; then
    echo "Copying all batch orders."
else
    echo "please enter correct input."
fi

A space is needed after elif and before [ $answer -eq $no ]. The tests make string comparisons, not numeric comparisons. So, = is needed in place of -eq. So that the script works even if the user enters nothing, $answer is placed inside double-quotes in the tests. Also, a space is required between echo and "please enter correct input.".

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

3 Comments

yes,it worked.Thanks but i want to know why it was showing error for then?can you please explain
It was the lack of a space after elif that confused bash leading to the unexpected then error.
More specifically, [ is not part of bash's syntax; it's a command name (an alias for the test command), so bash doesn't consider elif[ to be the word elif followed by a [; it's a single word elif[.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.