Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I stop expr removing the leading zero

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
I am writing a script to archive some log files. It will run on the first day of the month and archive the files from 2 months ago i.e on November 1st it will archive Septembers files. The file name is of the form YYMMDD01.PAZ. The script takes the current month and then subtracts 2 using expr, unfortunately the value returned is missing the leading zero. Below is an extract of the script

if [ "$SOLMON" -ge 3 -o "$SOLMON" -le 12 ]
then
COMPMON=`expr $SOLMON - 2`

Is there a way to preserve the leading zero.

Thanks

Alan
 
Thanks, it works perfectly. I didn't think about printf, easy when you know how.

Alan
 
In the Korn shell you can aso define the environment variable to be two digits, zero filled. It will always be two digits with a leading zero.
Code:
typeset -Z2 COMPMON
(( COMPMON = SOLMON - 2 ))
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top