Home > Software design >  Why am I getting a "line 10: syntax error near unexpected token `fi'" error in bash s
Why am I getting a "line 10: syntax error near unexpected token `fi'" error in bash s

Time:10-05

I am trying to create a function in bash:

check_for_file()
{
if [ $1 = '' ]; then
        local testing_file = myproject
elif
        local testing_file = $1
fi

if [ -d testing_file ]; then
        echo "Directory name already exists"
        exit
elif
        mkdir -p testing_file/{archive,backups,docs/html,docs/txt,assets,database,src/sh,src/c}
fi
return
}

But when I run it I get the following output: ./mkproj.sh: line 10: syntax error near unexpected token fi' ./mkproj.sh: line 10: fi'

Do you have any idea what I'm doing wrong? Thank you!

CodePudding user response:

elif expects another command to test. Formatting aside, local testing_file = $1 is treated as that condition, but then the keyword fi is seen before the expected then keyword.

Use else instead:

if [ $1 = '' ]; then
        local testing_file=myproject
else
        local testing_file=$1
fi

(Note, too, that you cannot put spaces around the = in the assignments.

CodePudding user response:

You should use then after the elif statement But your statement is also wrong because elif needs a command to test

So in your case, just use the else instead.

  • Related