Maths in shell scripts

Hi,

Need help on this. I need to increment a variable by 1 but retain as 2 characters.

I am using expr to do additions:

NEWSERIAL=`expr $SERIAL + 1`

$SERIAL can range from 01-99. After adding "1", I need the result to be 2 characters, eg: 02+1 = 03. By default expr will truncate the zero such that 02+1 will become "3" not "03"

Please help. Much appreciated.

Thanks.

one way ..

$ cnt=3
$ echo $cnt
3
$ [ $cnt -lt 10 ] && cnt=0$((cnt))
$ echo $cnt
03
$

@jayan_jay: Wouldn't $cnt become 099, if [ $cnt -le 99 ] were in a loop incrementing $cnt by one each time?

@vchee: Try this.

NEWSERIAL=`printf "%02d" $(($SERIAL + 1))`

Thanks for all the help. It works.

@balaje, yes correct.. while using in loop it might get fail .. Thanks .. :smiley: