shell Git预提交错误:检查文件名:.git/钩子/预提交;第25行;语法错误:意外的文件结尾

xlpyo6sf  于 2022-11-16  发布在  Shell
关注(0)|答案(1)|浏览(173)

下面是我在VS 2019中用于预提交的钩子代码。我想限制一些文件总是被提交。尝试使用string.exe。但是得到错误。需要帮助。

#!/bin/sh
#!/usr/bin/env bash
listchange=$(git diff --name-only) 

echo "Check filenames:"

wrongfilenames=$(
            for filename in $listchange 
            do
                fname= $filename
                if [[ $fname = 'strings.exe' ]]; then 
                    echo $fname;
                    echo "has strings.exe"
                fi
            done
        );
        
if [ -n "$wrongfilenames" ] ; then
echo "The following files need not to commit:"
echo "$wrongfilenames"
echo "Commit rejected, fix it and try again."
exit 1
else
echo "....OK"
zqry0prt

zqry0prt1#

在条件检查的末尾似乎缺少fi

#!/bin/bash
listchange=$(git diff --name-only) 

echo "Check filenames:"

wrongfilenames=$(
    for filename in $listchange 
    do
        if [[ "$filename" = "strings.exe" ]]; then 
            echo "$filename"
            echo "has strings.exe"
            # TODO: take the appropriate action(s) here
            return 1 # => quit with error
        fi
    done
);
        
if [ -n "$wrongfilenames" ] ; then
    echo "The following files need not to commit:"
    echo "$wrongfilenames"
    echo "Commit rejected, fix it and try again."
    exit 1
else
    echo "....OK"
fi

注意:在检查字符串内容时,最好在$var前后加上双引号。实际上,如果$var有空格,它将无法正常工作。

相关问题