[PLUG] looping over file names in numeric order in bash?

Steve Beattie sbeattie at suse.de
Wed Feb 7 20:13:30 UTC 2007


On Wed, Feb 07, 2007 at 10:07:03AM -0800, Paul Mullen wrote:
> On Wed, Feb 07, 2007 at 09:38:44AM -0800, Galen Seitz wrote:
> > Given the following file names, is there an easy way to loop
> > over them in numeric order?
> > 
> > data1.ps   data11.ps  data3.ps  data5.ps  data7.ps  data9.ps
> > data10.ps  data2.ps   data4.ps  data6.ps  data8.ps
> 
> Use 'seq', which generates a sequence (shocking!). It's an
> indispensable tool for shell script looping.
> 
> for i in $(seq 1 11); do
> 	echo data${i}.ps
> done

While shelling out to seq is more portable, bash will also let you do:

  for ((i = 1; i <= 11; i++)); do
  	echo data${i}.ps
  done

Also, seq -w is handy when files have been named "properly" with leading
zeros.

-- 
Steve Beattie
SUSE Labs, Novell Inc. 
<sbeattie at suse.de>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20070207/3edcd2c6/attachment.asc>


More information about the PLUG mailing list