Menu

Bash script - single & double digit problem.

David Wall
2021-09-15
2021-09-15
  • David Wall

    David Wall - 2021-09-15

    I know it's not COBOL but I tried to put it in a bash forum with nil result (couldn't get access).
    I'm trying to use a variable that ranges from 1 to 99 as part of a filename in a script as follows:
    1. the variable - dw=0
    2. Increment it- ((dw++))
    3. Now use it - filename=david$dw

    I 'would have' expected david01, david 02 etc: but obviously get david1, david2 etc:
    I've tried adding printf -v dw "%02d" $dw with no change
    I've tried also filename=david\$(( 10#\$dw )) also with no change

    I get names like david7, david8 & david9 followed by david10 so it's not thinking the variable is octal.

    Any smart ideas as to how to make the variable retain the leading 0 ??.

     
  • David Wall

    David Wall - 2021-09-15

    Yep - Thanks Simon - the last one worked.
    Keeping it all in the one statement was the trick - I was doing the printf & THEN using the $dw in the filename which resulted in going back to the single digit.

     

Anonymous
Anonymous

Add attachments
Cancel