0

这个想法是为目录中的所有文件(包括所有子目录)计算 SHA256 哈希,但排除另一个文本文件中指定的一些文件。

问题是,如果我指定要排除以下文件(参见代码下方),则只排除其中一个,而不是同时排除两者。

这是我的代码:

while read line
do
    if [ $line_count -eq 0 ]
    then
        exclude_files=".*/$line$"
    else
        exclude_files="${exclude_files}\|.*/$line$"
    fi

    line_count=$(( $line_count + 1 ))
done < exclude-files.txt

find . -type f -print0 | xargs -0 shasum -a 256 | grep -v -P "${exclude_files}" > ~/out.txt

文件内容exclude-files.txt

Icon\\r
.DS_Store
--- empty line ---

该文件Icon\r是用于更改文件夹图标的特殊文件,其名称包含CR. (我在 Mac OS X 10.7.4 上)

4

2 回答 2

2

这是因为在您的变量\中被识别为转义符号|

exclude_files="${exclude_files}\|.*/$line$"

您需要添加其他\人才能逃脱\以使其工作:

exclude_files="${exclude_files}\\|.*/$line$"

此外,您正在-P使用grep. 在这种情况下,您不需要 escape |。因此,您完全不用反斜杠就可以使用它。

您应该选择使用哪种方式:escape 或-P. 两个一起用是不行的。

于 2012-06-29T10:17:38.283 回答
0

如果文件名包含具有特殊含义的字符,则 grep 将不安全,也许这会有所帮助

cmd=(find . -type f \( )
while read line;do cmd=("${cmd[@]}" \! -name "$line" -a);done < exclude-files.txt
cmd[${#cmd[*]}-1]=\)
echo "${cmd[@]}" | cat -v
"${cmd[@]}" -print0 | xargs -0 shasum -a 256
于 2012-06-29T11:15:47.423 回答