0

我编写了一个简单的脚本来检查 zip 文件的内容与控制文件的内容。

它运行良好,但是当我收到其中包含空格的文件时,它会因错误而失败(实际上并不存在)。这是我的代码片段(name是为批量处理 ZIP 文件而创建的数组)。

echo "`date '+%m/%d/%y %T:'` List ZIP file contents."                                          
LIST_Array=(`/usr/bin/unzip -l $name | head -n -2|tail -n +4 | sort -r | awk '{print $4}'`)
LISTlen=${#LIST_Array[*]}
#iterate array to 1) build report and 2) look for control file
echo "`date '+%m/%d/%y %T:'` Iterate array to 1) build report and 2) look for control files."  
echo -e "`date '+%m/%d/%y %T:'` Files in ZIP file: $name\n"                                    >> $name.report.out
for (( i = 0 ; i < ${#LIST_Array[@]} ; i++ ))
do
    echo -e "${LIST_Array[$i]}"      >> $name.report.out
done

ZIP 中的文件列表被捕获$name.report.out,然后与控制文件本身的内容进行比较。

如何正确显示带空格的文件?我虽然echo -e会有所帮助,但似乎没有效果。

谢谢。

4

1 回答 1

2

所以 zip 文件中有一些文件,其中一些文件的名称中有空格。在这种情况下,当您列出文件时, awk '{ print $4 }' 不会捕获整个文件名。read很好,因为它的最后一个参数捕获了该行的其余部分:

LIST_Array=()
while read length date time filename; do
    LIST_Array+=( "$filename" )
done < <(/usr/bin/unzip -qql "$name")
于 2012-07-23T23:02:18.547 回答