progressive number whith 2 digit

progressive number whith 2 digit

Post by Sapien » Mon, 14 Feb 2005 05:54:43



Hi all,
I'm doing a simple backup shell script,
I want to create a tarball filename like ${DIR}_${DATE}${PN}.tgz
PN must be a two digit progressive number but I have some problems with
the first zero.

#!/bin/sh
DATE=`date '+%Y%m%d`
PN=00
VHOST=/www/vhosts/www.domain.tld
while [ -f $VHOST/backup/htdocs_${DATE}${SERIAL}.tgz ] ; do
         PN=`expr $PN + 01`
done
tar -zcf $VHOST/backup/htdocs_${DATE}${PN}.tgz -C $VHOST htdocs
#EOF

Then i try to some backup
$ ./backup.sh ; ./backup.sh ; ./backup.sh
$ ls -1 htdocs*
htdocs_2005021200.tgz
htdocs_200502121.tgz
htdocs_200502122.tgz

So, as you can see the PN is only one digit.
00
1
2
but must be
00
01
02

There is anyone that know how to do it?
Thanks in advance and sorry for my english,
Federico.

 
 
 

progressive number whith 2 digit

Post by Sapien » Mon, 14 Feb 2005 06:00:38



> #!/bin/sh
> DATE=`date '+%Y%m%d`
> PN=00
> VHOST=/www/vhosts/www.domain.tld
> while [ -f $VHOST/backup/htdocs_${DATE}${SERIAL}.tgz ] ; do
>         PN=`expr $PN + 01`
> done
> tar -zcf $VHOST/backup/htdocs_${DATE}${PN}.tgz -C $VHOST htdocs
> #EOF

Sorry, the SERIAL variable must be replaced with PN,
follow the correct script:

#!/bin/sh
DATE=`date '+%Y%m%d`
PN=00
VHOST=/www/vhosts/www.domain.tld
while [ -f $VHOST/backup/htdocs_${DATE}${PN}.tgz ] ; do
         PN=`expr $PN + 01`
done
tar -zcf $VHOST/backup/htdocs_${DATE}${PN}.tgz -C $VHOST htdocs
#EOF

 
 
 

progressive number whith 2 digit

Post by Chris F.A. Johnso » Mon, 14 Feb 2005 06:10:42



> Hi all,
> I'm doing a simple backup shell script,
> I want to create a tarball filename like ${DIR}_${DATE}${PN}.tgz
> PN must be a two digit progressive number but I have some problems with
> the first zero.

> #!/bin/sh
> DATE=`date '+%Y%m%d`
> PN=00
> VHOST=/www/vhosts/www.domain.tld
> while [ -f $VHOST/backup/htdocs_${DATE}${SERIAL}.tgz ] ; do
>          PN=`expr $PN + 01`

   If you are using a POSIX shell (e.g. bash or ksh), there's no need
   for expr:

PN=$(( $PN + 1 ))

   But if you are padding the variable, you should use:

PN=$(( ${PN#0} + 1 ))

Quote:> done
> tar -zcf $VHOST/backup/htdocs_${DATE}${PN}.tgz -C $VHOST htdocs
> #EOF

> Then i try to some backup
> $ ./backup.sh ; ./backup.sh ; ./backup.sh
> $ ls -1 htdocs*
> htdocs_2005021200.tgz
> htdocs_200502121.tgz
> htdocs_200502122.tgz

> So, as you can see the PN is only one digit.
> 00
> 1
> 2
> but must be
> 00
> 01
> 02

> There is anyone that know how to do it?

case $PN in
     ?) PN=0$PN ;;
esac

   Or:

[ $PN -lt 10 ] && PN=0$PN

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2005, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

progressive number whith 2 digit

Post by Bill Seiver » Mon, 14 Feb 2005 08:30:55



> Hi all,
> I'm doing a simple backup shell script,
> I want to create a tarball filename like ${DIR}_${DATE}${PN}.tgz
> PN must be a two digit progressive number but I have some problems with
> the first zero.

> #!/bin/sh
> DATE=`date '+%Y%m%d`
> PN=00
> VHOST=/www/vhosts/www.domain.tld
> while [ -f $VHOST/backup/htdocs_${DATE}${SERIAL}.tgz ] ; do
>         PN=`expr $PN + 01`

Add:      PN=`expr "00$PN" : '.*\([0-9][0-9]\)$`
Quote:> done
> tar -zcf $VHOST/backup/htdocs_${DATE}${PN}.tgz -C $VHOST htdocs
> #EOF

 
 
 

1. How to find the what number is missing in Sequence of 19 or 20 digit number.

I got a data file with 19 or 20 digit number in sequence, i need to
check what are the number's were missing in that sequence.
Ex:
89014102069901080256
89014102069901080264
89014102069901080272
89014102069901080280
89014102069901080298
89014102069901080306
89014102069901080314
89014102069901080322
89014102069901080330
89014102069901080348
89014102069901080355
89014102069901080363
89014102069901080371
89014102069901080389
89014102069901080397
89014102069901080405
89014102069901080413
89014102069901080421
89014102069901080439
89014102069901080447

Thanks
Chan

2. interface errors

3. changing one-digit month/day strings to two digits

4. Message Passing

5. PPP WITH A 10 DIGIT NUMBER!

6. ATI Radeon 7000 VGA IOBase moving around

7. How to separate numbers in three-digit groups in C

8. Linux box temporarily stops responding to IP

9. Reg Exp to match specified number of digits

10. entering only a 2 digit number

11. vsscanf do not convert hex numbers starting with a non-digit

12. How to best round a double to a given number of significant digits?

13. double digit partition numbers