Score:0

Bash script buildscript error. Line 20: syntax error near unexpected token 'fi'

fm flag

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
kh flag
Look at the `if sudo cp -r dist/* /home/ec2-user/testfolder; then` if block... where's the `else` vs the `fi`?
Ashok S avatar
fm flag
How the script should be? Can someone correct the scripts?
kh flag
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?
ph flag
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](https://www.codeproject.com/articles/626403/how-and-why-to-avoid-excessive-nesting).
vidarlo avatar
ar flag
@GordonDavisson Why not post that comment as an answer?
Score:1
us flag

in the middle replace this :

        fi
        else
        echo "deployment failed"

by :

        else
           echo "deployment failed"
        fi
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.