LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2006, 12:57 AM   #1
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Rep: Reputation: 0
how to loop a command?


does anyone know the basic syntax to loop a command?

i have the following line that finds and unzips .zip files to the directory it finds them.

find . -name "*.zip" -exec /home/knoppix/blah {} \;

however, on the chance that the zip files contain other zip files and so on, i want to unzip those as well.

How would i go about looping this command until no zip files are left to unzip?

Cheers
 
Old 06-22-2006, 03:26 AM   #2
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
Can I take a stab in the dark? I'm no programmer, so this is probably a silly hack, but you could try the "watch" command:
Code:
watch -n60 'find . -name "*.zip" -exec /home/knoppix/blah {} \;'
I've used that when working in a term and needing to watch a CPU temp to see how compiling was effecting it like this
Code:
watch -n5 'cat /proc/acpi/thermal_zone/THZN/temperature'
Issue and read "man watch" to see if that's applicable, and read about the n switch -- I used -n5 (5 seconds).
 
Old 06-22-2006, 03:28 AM   #3
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
Well you could move your zip file to another directory everytime you inzipped it and check if there are anymore zips available.

Code:
while ls *.zip >/dev/null
do
  for i in *.zip
  do
    # unzip $i
    # move $i to another directory
  done
done
This piece of shell script should do something alike. You need the nested loop, because the contents of your directory changes after every iteration. So if you would only loop the innermost loop you would only extract the zips currently in the directory. The new zips would not get extracted, so the outermost loop ensures everytime a new zip pops up, it will get extracted.

Of course it would be better to not move the old zip files(), but extract the first zip to some directory and only extract and (re)move the new zips from there.

good luck,
elluva
 
Old 06-22-2006, 03:31 AM   #4
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
Quote:
Originally Posted by Chinaman
...but you could try the "watch" command:
Code:
watch -n60 'find . -name "*.zip" -exec /home/knoppix/blah {} \;'
...
No, this will not work, because it will extract your initial zips over again everytime watch calls the given command.

good try though .
 
Old 06-22-2006, 03:40 AM   #5
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
Thanks for being gracious and explaining it. Wouldn't hurt for me to take a month off and study that bash scripting book I bought last year, eh?
 
Old 06-22-2006, 04:21 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by elluva
No, this will not work, because it will extract your initial zips over again everytime watch calls the given command.

good try though ;).
Actually it wouldn't because adamrosspayne's blah rm's
the zips after unpacking them :}

The problem with the watch approach though is that it doesn't
WAIT until one of the finds is finished. You may get into a race-
condition in unpacking/deleting :}

Something like (I'm using an arbitrary value of 3 iterations here, but
everyone in their right mind knows that zipping zips repeatedly makes
the final zip BIGGER)
Code:
for i in 1 2 3; 
do
 find . -name "*.zip" -exec /home/knoppix/blah {} \;
done

Cheers,
Tink
 
Old 06-22-2006, 08:19 PM   #7
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Original Poster
Rep: Reputation: 0
you guys are sharp, ive taken some knowledge from a few people and come up with:

while ls -R | grep [.]zip >\dev\null; do (sudo find . -name "*.zip" -exec /home/knoppix/blah {} \; done


im not sure how exactly but it just works :-)
 
Old 06-22-2006, 08:21 PM   #8
adamrosspayne
LQ Newbie
 
Registered: Jun 2006
Posts: 19

Original Poster
Rep: Reputation: 0
by the way that smiley face was actually ... {} \ ; ) ; done
 
Old 06-22-2006, 08:25 PM   #9
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
Quote:
Originally posted by adamrosspayne
by the way that smiley face was actually ... {} \ ; ) ; done
Leave out the spaces, but use code tags to display it like this:
Code:
while ls -R | grep [.]zip >\dev\null; do (sudo find . -name "*.zip" -exec /home/knoppix/blah {} \;); done
Your line here is incorrect because you put spaces to kill the emoticon.

Last edited by Bruce Hill; 06-22-2006 at 08:38 PM.
 
Old 06-22-2006, 08:38 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by adamrosspayne
you guys are sharp, ive taken some knowledge from a few people and come up with:

while ls -R | grep [.]zip >\dev\null; do (sudo find . -name "*.zip" -exec /home/knoppix/blah {} \; done


im not sure how exactly but it just works :-)
That will have as many iterations of the find run as there are zips in the
directory structure... :}


I don't know what the nesting-level for your zips may be, but
I DO hope it's not that many ;}


Cheers,
Tink
 
Old 06-23-2006, 08:52 AM   #11
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
Quote:
Originally Posted by Tinkster
That will have as many iterations of the find run as there are zips in the
directory structure... :}
Since that's exactly what he asked for, it isn't really a problem, is it? Very few zip files have more than a 2 or 3 levels of nesting anyway.


elluva.
 
Old 06-23-2006, 04:40 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by elluva
Since that's exactly what he asked for, it isn't really a problem, is it? Very few zip files have more than a 2 or 3 levels of nesting anyway.


elluva.
He asked for all levels to be expanded; he didn't ask for it to
attempt e.g 100 iterations if there happen to be 100 zips in
the directory structure (none of which will -hopefully- have a
100 levels of zips nested).

It's not a "problem", anyway, it just does way more work than I
think is necessary to solve his problem.


Cheers,
Tink
 
Old 06-23-2006, 05:36 PM   #13
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
There won't be an iteration for each zip file. Note that grep is only used to check if there's a zip at all, it's not used to feed the loop with each filename separately.
 
Old 06-23-2006, 06:04 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Good point; it will actually be an endless loop :}
Wish I had spotted that before.


Cheers,
Tink
 
Old 06-23-2006, 07:00 PM   #15
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Talking

Sorry about that, but it won't be an endless loop if /home/knoppix/blah really unzips the files it is given. For then, the zip files processed will have vanished when the next "ls -R | grep [.]zip" is issued.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: command works but not inside of for loop. RijilV Programming 3 05-21-2006 09:29 PM
Loop Bash command to hit... rumperpumper Linux - General 4 01-31-2006 09:39 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 06:17 PM
How to perform an action (a command) on multiple files with the FOR loop? chun-mee Programming 4 08-23-2005 04:11 AM
while-loop Thomas23 Programming 4 05-24-2004 04:35 PM


All times are GMT -5. The time now is 06:29 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration