0

In my bash script I'm getting an error:

./buildscriptbuild.sh: line 20: syntax error near unexpected token 'fi'

I have tested the below bash script in my local system. Can anyone help me in this regard. Thanks in advance.

#!/bin/sh

cd /home/ec2-user/inoutserver
if git pull origin development; then
    if npm install; then
        if grunt build --force; then
            echo "build success"
        else
            if sudo cp -r dist/* /home/ec2-user/testfolder; then
                echo "deployment success!"
            fi
            else
            echo "deployment failed"
        fi
    else
        echo "build failed"
    fi
else
    echo "npm install failed"
fi
5
  • Look at the if sudo cp -r dist/* /home/ec2-user/testfolder; then if block... where's the else vs the fi?
    – JohnLBevan
    Commented Nov 18, 2022 at 10:18
  • How the script should be? Can someone correct the scripts?
    – Ashok S
    Commented Nov 18, 2022 at 10:25
  • Look at the pattern followed by every other if/else statement in your code; what's different about those to the line I pointed out above?
    – JohnLBevan
    Commented Nov 18, 2022 at 10:50
  • You have an if ... fi ... else sequence that really shouldn't be that way. The mistake would be a lot more obvious if you fixed the indentation, so that corresponding if, else, and fi keywords were at the same indentation level. Also, it'd be a lot clearer to rewrite this with the error conditions first, as an if ! step1command; then echo "step 1 error"; elif ! step2command; then echo "step 2 error"; elif ! step3command;... See this. Commented Nov 19, 2022 at 2:18
  • @GordonDavisson Why not post that comment as an answer?
    – vidarlo
    Commented Nov 19, 2022 at 19:26

1 Answer 1

1

in the middle replace this :

        fi
        else
        echo "deployment failed"

by :

        else
           echo "deployment failed"
        fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .