LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Listing contents of *.zip file into an array (http://www.linuxquestions.org/questions/programming-9/bash-listing-contents-of-%2A-zip-file-into-an-array-808707/)

cade 05-18-2010 06:31 PM

BASH: Listing contents of *.zip file into an array
 
Hi, I am fairly new to programming and I need some help with my script.
I want to list the contents of a zip file amd put each entry into an array.
I've been doing
Code:

array=($(unzip*-qq*-l*"/path/to/file.zip"*|*awk*'{print*$4}'))
which works alright, and when I do
Code:

  for str in ${array[@]}; do
      echo "$str"
  done

It gives
Code:

  asdf/something
  base/asdf/something/the_quick_brown_fox_jumped_over_the_lazy_dog

.. until an item has a space in it's name
Code:

  for str in ${array[@]}; do
      echo "$str"
  done

Gives
Code:

  asdf/something
  base/asdf/something/the
  quick
  brown
  fox
  jumped
  over
  the
  lazy
  dog

Thanks :)

grail 05-18-2010 08:57 PM

Try a while/read loop instead:
Code:

while read line
do
<your stuff here>
done< <(<your unzip stuff here>)

Be aware of the space between the two signs <, after done

ta0kira 05-18-2010 09:06 PM

You need to use a different field separator ($IFS) that excludes spaces:
Code:

OLD_IFS="$IFS"
IFS=$'\n'
array=($(zip stuff...))
IFS="$OLD_IFS"

#...

for I in `seq 0 1 $((${#array[*]}-1))`; do
  #stuff with ${array[$I]}
done

Kevin Barry

ntubski 05-18-2010 09:35 PM

Code:

  for str in "${array[@]}"; do
      echo "$str"
  done

EDIT: this assumes array has been set correctly

ta0kira 05-19-2010 04:13 PM

Quote:

Originally Posted by ntubski (Post 3973500)
Code:

  for str in "${array[@]}"; do
      echo "$str"
  done


Thanks. I'm assuming this is just a correction to my loop and not to the IFS part.
Kevin Barry


All times are GMT -5. The time now is 09:07 AM.