0

I can't figure out what is wrong with following script.

#!/bin/bash 
if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
else if [ "$1" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi

I am on linux (Ubuntu) in xterm.

4
  • You'd better use the elif keyword in bash (help if for more info). Commented Dec 11, 2012 at 18:02
  • I am surprised that bash could not produce a better error. Commented Dec 11, 2012 at 18:09
  • my bash told me to look for the error in line#11, which is exactly where the fi was missing Commented Dec 11, 2012 at 18:11
  • @umlaeute I would have expected missing fi in addition of line no in this case. Commented Dec 11, 2012 at 19:15

4 Answers 4

3

you are missing closing "fi" at the end. the else if construct really is not an elif continuation, but instead the new if lives within the else clause of the previous if.

so, properly formatted you should have:

#!/bin/bash 
if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
else 
  if [ "$1" = "y" ] 
  then 
    echo Pulling web on sharada from origin.
  else 
    echo "Usage : arg x to push or y to pull."
  fi
fi # <-- this closes the first "if"
Sign up to request clarification or add additional context in comments.

Comments

2

It should be elif, not else if, as shown below:

if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
elif [ "$1" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi

Comments

1

You have two ifs, therefore you need two fis.

Comments

0

The problem described in the questioned has already been solved. I just wanted to share my experience here because the error message I got was the same, even if the reason and the solution was slightly different.

#!/bin/bash

echo "a"

if [ -f x.txt ]; then \
    echo "b" \
fi;

echo "c"

This script produced the same error message and the solution was to add the missing semicolon to the end of the echo "b" line:

#!/bin/bash

echo "a"

if [ -f x.txt ]; then \
    echo "b" ; \
fi;

echo "c"

The direct reason was the same as it was in the original question. The if didn't have its closing pair fi. The reason, however, was that the fi line blended into the previous line, due to the missing ; semicolon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.