On Apr 2, 8:19 am, Javier Montoya <jmonto...@gmail.com> wrote: > On Mar 19, 6:04 pm, DennisW <dennistwilliam...@gmail.com> wrote: > > > > > > > On Mar 19, 6:50 am, Javier Montoya <jmonto...@gmail.com> wrote: > > > > On Mar 19, 12:39 pm, Javier Montoya <jmonto...@gmail.com> wrote: > > > > > Dear all, > > > > > 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. An example of the content of a > > > > directory is given below: > > > > /bla/bla/bla/dir1 > > > > -> 00000.jpg > > > > -> 00001.jpg > > > > -> > > > > -> 09001.jpg > > > > > The desired output would be the integer numbers: 0 and 9001 > > > > 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' | > > > > sed -e 's|^.*/||' -e 's/\.[^.]*$//')) > > > > in our example, I obtain sth. like: > > > > seqInterval[0] is equal to "00000" and seqInterval[1] is equal to > > > > "09001" > > > > Since those are string numbers, I was wondering which is the best way > > > > to convert those strings into integer numbers (seqInterval[0]=0 and > > > > seqInterval[1]=9001)? > > > > > I tried the code below, but it doesn't work properly, any suggestions? > > > > startImg=$(printf '%d' ${seqInterval[0]}) > > > > endImg=$((printf '%d' ${seqInterval[1]})) > > > > > Best whishes > > > > Dear all, > > > > I found a solution: > > > > startImg=$(printf '%d' ${seqInterval[0]//0/}) > > > endImg=$(printf '%d' ${seqInterval[1]//0/}) > > > > Is there's a better way, pls let me know! > > > In the case of 09001 your command results in "91" > > > Try: > > > shopt -s extglob > > printf '%d' ${seqInterval[0]/#*(0)/} > > printf '%d' ${seqInterval[1]/#*(0)/} > > > which says "delete a sequence of zero or more '*()' zeros anchored at > > the beginning '#' of the string" > > Thanks Dennis! But in the case of 000000, I obtain an empty value. > Ideally, I should keep just one zero. > > Best wishes
$ shopt -s extglob $ declare -a 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 Tested in Bash versions 3.2.49(23)-release and 4.0.33(1)-release.