home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / shell / 5467 < prev    next >
Encoding:
Text File  |  1993-01-23  |  1.7 KB  |  54 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!mcdchg!laidbak!katzung
  3. From: katzung@i88.isc.com (Brian Katzung)
  4. Subject: Re: Is there a "foreach" loop control in ksh?
  5. Message-ID: <1993Jan22.173754.17016@i88.isc.com>
  6. Sender: usenet@i88.isc.com (Usenet News)
  7. Nntp-Posting-Host: laitnite.i88.isc.com
  8. Organization: Interactive Systems Corporation, Naperville, IL
  9. References: <1304@alsys1.aecom.yu.edu>
  10. Date: Fri, 22 Jan 1993 17:37:54 GMT
  11. Lines: 41
  12.  
  13. In article <1304@alsys1.aecom.yu.edu> manaster@yu1.yu.edu (Chaim Manaster) writes:
  14. |I want to do a series of comands on each file in the current
  15. |directory, with some sort of "foreach" construct that will loop
  16. |over every file in some list (something like in perl), or in some
  17. |cases every file in the current directory. How would one do this in
  18. |ksh?
  19.  
  20. For every file except, regardless of type, try:
  21.  
  22.     for file in `find . -print -prune`
  23.     do
  24.         ...
  25.     done
  26.  
  27. For regular files, use:
  28.  
  29.     for file in `find ..?* .[!.]* * -prune -type f -print 2> /dev/null`
  30.     do
  31.         ...
  32.     done
  33.  
  34. You can also use the one above without the "-type f" if you want
  35. all types of files, but don't want ./ in front of each one.  Just
  36. omit the "." patterns if they are excessive for your needs.
  37.  
  38. |In particular, I want to run compress on each file in the directory
  39. |that does not already end in a .Z
  40.  
  41.     find ..?* .[!.]* * -prune ! -type f -name '*.Z' -print 2> /dev/null |
  42.     xargs compress
  43.  
  44. |(dito for uncompress in
  45. |reverse).
  46.  
  47.     find ..?* .[!.]* * -prune -type f -name '*.Z' -print 2> /dev/null |
  48.     xargs uncompress
  49.  
  50. There is nothing ksh-specific here BTW, so these should work in sh just
  51. as well.
  52.  
  53.   -- Brian Katzung  katzung@i88.isc.com
  54.