LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2010, 06:31 PM   #1
cade
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
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

Last edited by cade; 05-18-2010 at 06:37 PM.
 
Old 05-18-2010, 08:57 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 05-18-2010, 09:06 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-18-2010, 09:35 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
   for str in "${array[@]}"; do
      echo "$str"
   done
EDIT: this assumes array has been set correctly

Last edited by ntubski; 05-20-2010 at 09:25 AM. Reason: confirm ta0kira's assumption
 
Old 05-19-2010, 04:13 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
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
 
  


Reply

Tags
array, bash, list, unzip



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Listing Zip file contents prasanta_sadhukhan Linux - General 1 10-20-2009 07:43 AM
Bash script for listing FTP usage as the file name of a file created in each share jojothedogboy Programming 1 12-05-2008 03:35 PM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 03:13 PM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration