为什么我在 bash 脚本中收到 "line 10: syntax error near unexpected token `fi'" 错误?

Why am I getting a "line 10: syntax error near unexpected token `fi'" error in bash script?

我正在尝试在 bash 中创建一个函数:

check_for_file()
{
if [  = '' ]; then
        local testing_file = myproject
elif
        local testing_file = 
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
}

但是当我 运行 它时,我得到以下输出: ./mkproj.sh:第 10 行:意外标记附近的语法错误 fi' ./mkproj.sh: line 10: fi'

你知道我做错了什么吗?谢谢!

elif 需要另一个命令来测试。除了格式化,local testing_file = 被视为该条件,但是关键字 fi 出现在预期的 then 关键字之前。

改用else

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

(另请注意,您不能在作业中的 = 周围放置空格。

您应该在 elif 语句之后使用 then 但是你的说法也是错误的,因为 elif 需要一个命令来测试

所以在你的情况下,只需使用 else 即可。