0

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.

1
  • If name is really an array there, you are only getting the first element of it. To expand a shell array, express it as `"${array[@]}". Commented Jul 23, 2012 at 23:04

1 Answer 1

2

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. read is nice in that its last argument captures the rest of the line:

LIST_Array=()
while read length date time filename; do
    LIST_Array+=( "$filename" )
done < <(/usr/bin/unzip -qql "$name")
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is not name. It is LIST_Array where I unzip the zip file.
@Chris So you are saying the file names with spaces are the files inside the zip file?
I think you're using name for two different things here; as the array(?) containing the name(s) of the zip file(s) to read with unzip, and as the variable that read populates while reading the list of files inside the zip file.
@chepner that's a good catch. Thanks. (I've edited the answer, but for what it's worth, I suspect it would've worked in its original form – the old name would've been parsed before it got replaced.)
@kojiro: that is correct. The file names with spaces are the files inside the zip file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.