0

Below is the simple script, while running the script i am getting the exception like line 10: syntax error: unexpected end of file. please help.

#!/bin/sh
a=10
b=20
if [ 10 == 20 ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi
3

change == to -eq:

#!/bin/sh
a=10
b=20
if [ 10 -eq 20 ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

BTW: You have set the variables a and b, but you are not using them. I suppose you intended:

#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

See man test for what the expression in the [ ] looks like. = is for string comparisons, -eq, -gt, etc. are for integer comparisons.

  • 1
    Another BTW: if you replace /bin/sh by /bin/bash your script is working. – muclux May 10 '18 at 8:43
2

First, I believe [ 10 == 20 ] is a typo and it should be [ "$a" == "$b" ]. Second @dsstorefile1 is right In POSIX sh, == is not supported.

If you use shellcheck you can find this out yourself:

$ shellcheck script.sh 

In script.sh line 4:
if [ "$a" == "$b" ]
          ^-- SC2039: In POSIX sh, == is not supported.

Here is a bit advanced version of your script that could handle the first two positional parameters:

#!/bin/sh
a="$1"
b="$2"
if [ "$a" = "$b" ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

Then you can use it in this way:

$ ./script.sh 10 20
a is not equal to b

$ ./script.sh 10 10
a is equal to b
  • ./duke.sh 10 20 ./duke.sh: line 9: syntax error near unexpected token fi' ./duke.sh: line 9: fi' – Abhishek Gowlikar May 10 '18 at 7:25
  • there is no statement after fi statement. – Abhishek Gowlikar May 10 '18 at 7:31
  • Please respond any one. – Abhishek Gowlikar May 10 '18 at 7:47
  • 1
    @AbhishekGowlikar likely there is something wrong with how you are entering / running the script - such as Windows line endings or other non-printing characters – steeldriver May 10 '18 at 8:06
  • 2
    @AbhishekGowlikar, you could thanks also by up-vote and (or) accept of the useful answers :-) Btw, Muclux is perfectly right in your case is much better to use the integer comparison operators. – pa4080 May 10 '18 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.