Transient bug? Syntax error: "elif" unexpected (expecting "then")


#1

Got this bug today in a CircleCI build at the “Checkout code” step: /bin/sh: 47: Syntax error: "elif" unexpected (expecting "then")

Full log:

#!/bin/sh
set -e

# Workaround old docker images with incorrect $HOME
# check https://github.com/docker/docker/issues/2968 for details
if [ "${HOME}" = "/" ]
then
  export HOME=$(getent passwd $(id -un) | cut -d: -f6)
fi

mkdir -p ~/.ssh

echo '
github.com ssh-rsa REMOVED
bitbucket.org ssh-rsa REMOVED
' >> ~/.ssh/known_hosts

(umask 077; touch ~/.ssh/id_rsa)
chmod 0600 ~/.ssh/id_rsa
(cat <<EOF > ~/.ssh/id_rsa
$CHECKOUT_KEY
EOF
)

# use git+ssh instead of https
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true

if [ -e /ci/project/.git ]
then
  cd /ci/project
  git remote set-url origin $CIRCLE_REPOSITORY_URL || true
else
  mkdir -p /ci/project
  cd /ci/project
  git clone $CIRCLE_REPOSITORY_URL .
fi

if [ -n "$CIRCLE_TAG" ]
then
  git fetch --force origin "refs/tags/${CIRCLE_TAG}"
else
git fetch --force origin master:remotes/origin/master
fi

if [ -n "$CIRCLE_TAG" ]
  git reset --hard $CIRCLE_SHA1
  git checkout -q $CIRCLE_TAG
elif [ -n "$CIRCLE_BRANCH" ]
then
  git reset --hard $CIRCLE_SHA1
  git checkout -q -B $CIRCLE_BRANCH
fi

git reset --hard $CIRCLE_SHA1
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts.

remote: Counting objects: 615, done.        
remote: Compressing objects: 100% (6/6), done.        
remote: Total 615 (delta 0), reused 1 (delta 0), pack-reused 609        
Receiving objects: 100% (615/615), 843.51 KiB | 0 bytes/s, done.
Resolving deltas: 100% (315/315), done.
Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.

/bin/sh: 47: Syntax error: "elif" unexpected (expecting "then")
Exited with code 2

#2

Hi.

You forgot to write the then after if

if [ -n "$CIRCLE_TAG" ]
    git reset --hard $CIRCLE_SHA1
    git checkout -q $CIRCLE_TAG
elif [ -n "$CIRCLE_BRANCH" ]
then
     git reset --hard $CIRCLE_SHA1
     git checkout -q -B $CIRCLE_BRANCH
fi

just put the them word

if [ -n "$CIRCLE_TAG" ]
then
    git reset --hard $CIRCLE_SHA1
    git checkout -q $CIRCLE_TAG
elif [ -n "$CIRCLE_BRANCH" ]
then
     git reset --hard $CIRCLE_SHA1
     git checkout -q -B $CIRCLE_BRANCH
fi

#3