%%问题已解决,下面的代码按预期的方式运行%%

我正在尝试在Bash中编写SVN预提交钩子,以测试传入文件的UTF-8编码。经过大量的字符串争夺来获取传入文件的路径并忽略了目录/图片/已删除文件等之后,我使用了“ svnlook cat”来读取传入文件并将其通过管道传输到“ iconv -f UTF-8”。之后,我使用$ {PIPESTATUS [1]}读取了iconv操作的退出状态。

我的代码如下所示:

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv

# The file endings to ignore when checking for UTF-8:
IGNORED_ENDINGS=( png jar )

# Prepairing to set the IFS (Internal Field Separator) so "for CHANGE in ..." will iterate
# over lines instead of words
OIFS="${IFS}"
NIFS=$'\n'

# Make sure that all files to be committed are encoded in UTF-8
IFS="${NIFS}"

for CHANGE in $($SVNLOOK changed -t "$TXN" "$REPOS"); do
    IFS="${OIFS}"
    # Skip change if first character is "D" (we dont care about checking deleted files)
    if [ "${CHANGE:0:1}" == "D" ]; then
        continue
    fi

    # Skip change if it is a directory (directories don't have encoding)
    if [ "${CHANGE:(-1)}" == "/" ]; then
        continue
    fi

    # Extract file repository path (remove first 4 characters)
    FILEPATH=${CHANGE:4:(${#CHANGE}-4)}

    # Ignore files that starts with "." like ".classpath"
    IFS="//" # Change seperator to "/" so we can find the file in the file path
    for SPLIT in $FILEPATH
    do
        FILE=$SPLIT
    done
    if [ "${FILE:0:1}" == "." ]; then
        continue
    fi
    IFS="${OIFS}" # Reset Internal Field Seperator

    # Ignore files that are not supposed to be checked, like images. (list defined in IGNORED_ENDINGS field above)
    IFS="." # Change seperator to "." so we can find the file ending
    for SPLIT in $FILE
    do
        ENDING=$SPLIT
    done
    IFS="${OIFS}" # Reset Internal Field Seperator
    IGNORE="0"
    for IGNORED_ENDING in ${IGNORED_ENDINGS[@]}
    do
        if [ `echo $IGNORED_ENDING | tr [:upper:] [:lower:]` == `echo $ENDING | tr [:upper:] [:lower:]` ] # case insensitive compare of strings
        then
            IGNORE="1"
        fi
    done
    if [ "$IGNORE" == "1" ]; then
        continue
    fi

    # Read changed file and pipe it to iconv to parse it as UTF-8
    $SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -t UTF-16 -o /dev/null

    # If iconv exited with a non-zero value (error) then return error text and reject commit
    if [ "${PIPESTATUS[1]}" != "0" ]; then
        echo "Only UTF-8 files can be committed (violated in $FILEPATH)" 1>&2
        exit 1
    fi
    IFS="${NIFS}"
done

IFS="${OIFS}"

# All checks passed, so allow the commit.
exit 0


问题是,每次我尝试提交带有“æøå” iconv之类的斯堪的纳维亚字符的文件时,都会返回错误(退出1)。

如果我禁用了脚本,请用“æøå”提交文件,将“ svnlook -t”和“ svnlook cat -t”中的-t(事务)更改为-r(修订版),然后使用修订版手动运行脚本的“æøå”文件编号,然后iconv(以及该脚本)返回出口0。

为什么svnlook cat -r会按预期工作(返回UTF-8编码的“æøå”字符串),而不是svnlook cat -t无法正常工作?

最佳答案

问题是,如果未选择任何输出编码,iconv显然表现出异常。

改变中

$SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -o /dev/null




$SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -t UTF-16 -o /dev/null


解决了问题,并使脚本表现出预期的效果:)

关于linux - 为什么从SVN事务而不是SVN修订版中读取时,iconv为什么会错误退出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8430925/

10-14 16:17