bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string to integer


From: Greg Wooledge
Subject: Re: string to integer
Date: Wed, 5 May 2010 08:20:44 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Apr 02, 2010 at 11:23:26AM -0700, DennisW wrote:
> > > > On Mar 19, 12:39 pm, Javier Montoya <address@hidden> wrote:
> > > > > I have a several directories with *.jpg images. The image files are
> > > > > named as ascending numbers and I would like to retrieve the lowest and
> > > > > largest image number in each directory.

files=(*.jpg)
lowest=${files[0]} lowest=${lowest%.jpg}
highest=${files[0]} highest=${highest%.jpg}
for f in "address@hidden"; do
  n=${f%.jpg}
  if ((10#$n < 10#$lowest)); then lowest=$n; fi
  if ((10#$n > 10#$highest)); then highest=$n; fi
done

> > > > > An example of the content of a
> > > > > directory is given below:
> > > > > /bla/bla/bla/dir1
> > > > > -> 00000.jpg
> > > > > -> 00001.jpg
> > > > > ->
> > > > > -> 09001.jpg

Well hell, that's even easier then.  They're already zero-padded and
therefore sorted.

files=(*.jpg)
lowest=${files[0]} lowest=${lowest%.jpg}
address@hidden:(-1)} highest=${highest%.jpg}

> > > > > The desired output would be the integer numbers: 0 and 9001

echo $((10#$lowest))
echo $((10#$highest))

> > > > > With the code below, I'm able to retrieve in "seqInterval" array,
> > > > > which is the lowest and largest image number as strings.
> > > > > seqDir="/bla/bla/bla/dir1"
> > > > > seqInterval=($(find $seqDir -name "*.jpg" | sort | sed -n -e '1p;$p' |

Do you need this to be recursive?  That changes a lot.  I'm going
to assume you don't, so I don't have to write yet another (completely
different) solution.

> seqInterval='([0]="000000" [1]="000001" [2]="009001" [3]="390001")'
> $ for i in {0..3}; do printf '%d\n' ${seqInterval[i]/#*(0)/}; done
> 0
> 1
> 9001
> 390001

for i in 000000 000001 009001 390001; do
  echo $((10#$i))
done




reply via email to

[Prev in Thread] Current Thread [Next in Thread]