I have written a simple script that checks the content of a zip file against the contents of a control file.
It works well but it fails with errors (that are not really there) when I receives files that have spaces in them. This is a snippet of my code (name is an array created to process ZIP files in bulk).
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
The list of files in the ZIP is captured into $name.report.out and then compared against the contents of the control file itself.
How can I correctly display files with spaces? I though the echo -e would help but it seemingly has not effect.
Thanks.
So the zip file has files in it some of whose names have whitespace in them. In that case, when you list the files, awk ‘{ print $4 }’ will not capture the entire filename.
readis nice in that its last argument captures the rest of the line: