git - Hook /接收后 : Syntax error: "(" unexpected (expecting "done")

标签 git bash

我正在 GIT 中编写接收后脚本。

我附上了它的最小版本,但也失败了:

generate_email()
{
    for user in $(git config --get-all notifications.users); do
        unset files_to_notify
        for filter in $(git config --get-all notifications.$user); do
            files_to_notify=" $files_to_notify $(git diff-tree --no-commit-id \
                --name-only -r $newrev | grep $filter) "
        done
        files_to_notify=( $files_to_notify )
        if [ -n "$files_to_notify" ]; then
            echo ${files_to_notify[*]}
        fi
    done
}

while read oldrev newrev refname
do
    generate_email $oldrev $newrev $refname
done

当我尝试推送到 git 服务器以调用此脚本时,我收到以下消息:

远程:hooks/post-receive:10:hooks/post-receive:语法错误:“(”意外(期望“完成”)

当我尝试在命令行中运行它时,脚本可以正常工作而不会出现此消息。

有什么想法吗?

谢谢

埃利亚希夫

最佳答案

两种可能性:

  1. #!/usr/bin/env bash 添加到脚本顶部,以确保脚本由 bash 运行,而不是由 sh 运行,因为您使用数组,所以是 bash 扩展。

  2. 停止使用该数组,因为您不会以任何无法轻松用简单的空格分隔字符串替换的方式使用它。这就是您首先构建数组的方式。

关于git - Hook /接收后 : Syntax error: "(" unexpected (expecting "done"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808114/

相关文章:

macos - 理解 bash echo

Bash/Sed - 多行 sed 操作打印行乱序

Git pull 中止

git - 在不克隆的情况下计算 GitHub 存储库中的行数

git - 之前尝试过错误地将文件夹放入 GitHub

bash - 使用别名覆盖内置命令

bash - 运行 bash 初始化脚本的 GNU Screen

git - 当强制推送到远程 git 存储库时,现有标签会发生什么?

git - 将不需要的文件添加到 .gitignore 后,我仍然会在 Github 上上传这些文件

linux - 如何在 bash 中精确打印一些文本