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 ??.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 ??.
As you want a series of of files with a sequence you may want to use a loop of
for dw in {01..99}orfor dw in $(seq -f "%02g" 1 99), also see https://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-widthYour printf trick also should work:
sh dw=0 ((dw++)) filename=david$(printf "%02d" $dw)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.