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
    Nov 18, 2022 at 10:18
  • How the script should be? Can someone correct the scripts?
    – Ashok S
    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
    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. Nov 19, 2022 at 2:18
  • @GordonDavisson Why not post that comment as an answer?
    – vidarlo
    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

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.