Increment variable with leading zero's

Increment variable with leading zero's

Post by Dave » Thu, 13 Oct 2005 04:20:39



Using the following snippet of scipt as an example, how do I increment
my variable without loosing the leading zero?

  MyVar=00
  while [ $MyVar -lt 20]
  do
    echo "MyVar is $MyVar"
    MyVar=`expr MyVar + 1 `
  done

I would like to see an output of:
 00
 01
 02 ...
 10
 11
 12 ...
 19

But what I see is:
 00
 1
 2 ...
 10
 11 ...
 19

Any suggestions?  I am willing to use a different shell and I am open
to using sed or awk.  

Thanks

Dave

 
 
 

Increment variable with leading zero's

Post by Janis Papanagno » Thu, 13 Oct 2005 04:35:01



> Using the following snippet of scipt as an example, how do I increment
> my variable without loosing the leading zero?

>   MyVar=00
>   while [ $MyVar -lt 20]
>   do
>     echo "MyVar is $MyVar"
>     MyVar=`expr MyVar + 1 `
>   done

> Any suggestions?  I am willing to use a different shell and I am open
> to using sed or awk.  

If you are willing to use ksh93...

     typeset -Z2 MyVar=0
     for (( MyVar=0 ; MyVar < 20 ; MyVar++ ))
     do
         echo "MyVar is $MyVar"
     done

Janis

 
 
 

Increment variable with leading zero's

Post by Janis Papanagno » Thu, 13 Oct 2005 04:39:28



> Using the following snippet of scipt as an example, how do I increment
> my variable without loosing the leading zero?

>   MyVar=00
>   while [ $MyVar -lt 20]
>   do
>     echo "MyVar is $MyVar"
>     MyVar=`expr MyVar + 1 `
>   done

> Any suggestions?  I am willing to use a different shell and I am open
> to using sed or awk.  

If you are willing to use ksh93 or bash...

     for (( MyVar=0 ; MyVar < 20 ; MyVar++ ))
     do
         printf "MyVar is %02d\n" "$MyVar"
     done

Janis

 
 
 

Increment variable with leading zero's

Post by Stephane CHAZELA » Thu, 13 Oct 2005 05:14:20


2005-10-11, 21:35(+02), Janis Papanagnou:

>> Using the following snippet of scipt as an example, how do I increment
>> my variable without loosing the leading zero?

>>   MyVar=00
>>   while [ $MyVar -lt 20]
>>   do
>>     echo "MyVar is $MyVar"
>>     MyVar=`expr MyVar + 1 `
>>   done

>> Any suggestions?  I am willing to use a different shell and I am open
>> to using sed or awk.  

> If you are willing to use ksh93...

>      typeset -Z2 MyVar=0
>      for (( MyVar=0 ; MyVar < 20 ; MyVar++ ))
>      do
>          echo "MyVar is $MyVar"
>      done

[...]

Not only ksh93. Also zsh, ksh88 and pdksh for -Z2 and zsh for
the for (( ... )).

--
Stphane

 
 
 

Increment variable with leading zero's

Post by Erik Max Franci » Thu, 13 Oct 2005 05:53:31



> Any suggestions?  I am willing to use a different shell and I am open
> to using sed or awk.  

man printf

--

San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   In time of war the devil makes more room in hell.
   -- (a German proverb)

 
 
 

Increment variable with leading zero's

Post by Dave » Thu, 13 Oct 2005 06:38:12


Thanks for the help everybody!  I learned a few new things today.
 
 
 

Increment variable with leading zero's

Post by John W. Krah » Thu, 13 Oct 2005 09:45:33



> Using the following snippet of scipt as an example, how do I increment
> my variable without loosing the leading zero?

>   MyVar=00
>   while [ $MyVar -lt 20]
>   do
>     echo "MyVar is $MyVar"
>     MyVar=`expr MyVar + 1 `
>   done

> I would like to see an output of:
>  00
>  01
>  02 ...
>  10
>  11
>  12 ...
>  19

> But what I see is:
>  00
>  1
>  2 ...
>  10
>  11 ...
>  19

> Any suggestions?  I am willing to use a different shell and I am open
> to using sed or awk.  

You could do it in perl:

$ perl -e'
$MyVar = "00";
while ( $MyVar lt "20" ) {
    print "MyVar is $MyVar\n";
    $MyVar++;
    }
'
MyVar is 00
MyVar is 01
MyVar is 02
MyVar is 03
...
MyVar is 16
MyVar is 17
MyVar is 18
MyVar is 19

Or:

$ perl -e'
for $MyVar ( "00" .. "20" ) {
    print "MyVar is $MyVar\n";
    }
'
MyVar is 00
MyVar is 01
MyVar is 02
MyVar is 03
...
MyVar is 16
MyVar is 17
MyVar is 18
MyVar is 19

John
--
use Perl;
program
fulfillment

 
 
 

Increment variable with leading zero's

Post by Chris F.A. Johnso » Thu, 13 Oct 2005 10:24:10



> Using the following snippet of scipt as an example, how do I increment
> my variable without loosing the leading zero?

>   MyVar=00
>   while [ $MyVar -lt 20]
>   do
>     echo "MyVar is $MyVar"
>     MyVar=`expr MyVar + 1 `
>   done

> I would like to see an output of:
>  00
>  01
>  02 ...
>  10
>  11
>  12 ...
>  19

> But what I see is:
>  00
>  1
>  2 ...
>  10
>  11 ...
>  19

> Any suggestions?  I am willing to use a different shell and I am open
> to using sed or awk.  

   In any POSIX shell:

MyVar=0
while [ $MyVar -lt 20 ]
do
    printf "MyVar is %02d\n" "$MyVar"
    MyVar=$(( $MyVar + 1 ))
done

   In awk:

awk 'BEGIN { while (MyVar < 20 ) printf "MyVar is %02d\n", MyVar++; exit }'

   In bash version 3:

printf "MyVar is %02d\n" {0..19}

   In William Park's extended bash:

printf "MyVar is %s\n" {00..19}

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

Increment variable with leading zero's

Post by Stephane CHAZELA » Thu, 13 Oct 2005 05:18:10


2005-10-11, 21:39(+02), Janis Papanagnou:

>> Using the following snippet of scipt as an example, how do I increment
>> my variable without loosing the leading zero?

>>   MyVar=00
>>   while [ $MyVar -lt 20]
>>   do
>>     echo "MyVar is $MyVar"
>>     MyVar=`expr MyVar + 1 `
>>   done

>> Any suggestions?  I am willing to use a different shell and I am open
>> to using sed or awk.  

> If you are willing to use ksh93 or bash...

>      for (( MyVar=0 ; MyVar < 20 ; MyVar++ ))
>      do
>          printf "MyVar is %02d\n" "$MyVar"
>      done

[...]

Or zsh.

But what's the point of advertising non-standard syntaxes, when
the standard ones work just as well:

MyVar=0
while [ "$MyVar" -lt 20 ]
do
  printf 'MyVar is %02d\n' "$MyVar"
  MyVar=$(($MyVar + 1))
esac

zsh:

print -l "MyVar is "{00..19}

;)

--
Stphane

 
 
 

Increment variable with leading zero's

Post by Gran » Thu, 13 Oct 2005 16:45:08



>But what's the point of advertising non-standard syntaxes, when
>the standard ones work just as well:

>MyVar=0
>while [ "$MyVar" -lt 20 ]
>do
>  printf 'MyVar is %02d\n' "$MyVar"
>  MyVar=$(($MyVar + 1))
>esac

s/esac/done/ ??  
Quote:

>zsh:

>print -l "MyVar is "{00..19}

>;)

 
 
 

Increment variable with leading zero's

Post by Janis Papanagno » Fri, 14 Oct 2005 01:34:19



> But what's the point of advertising non-standard syntaxes, when
> the standard ones work just as well:

A clearer syntax and the OP's statement "I am willing to use a
different shell".

YMMV.

Janis

 
 
 

1. xlC doesn't zero initialize fundamental type variables?

Hi there,

Anyone noticed that xlC doesn't zero initialize variables of
fundamental types when used with the default constructor?
That means all of the below end up being uninitialized garbage values:

int i = int(); // garbage
std::vector<int> v(1);
int j = v[0]; // garbage
typedef int* int_p;
int_p p = int_p(); // garbage

I found a fix, which is to use -qinitauto=00, but that means variables
where you don't use the default constructur get initialized too, which
can be a nasty bug-hider.

Thanks

Dylan

2. The Interner Adapter (TIA)

3. Floppy reports size as zero sectors, zero tracks, zero bytes

4. Video, Changing Refresh values

5. sed for change lead blank to zero

6. Sound Blaster 16 gateway 2000

7. ksh arithmetic has problems with leading zeroes?

8. ADB MouseMan Mouse

9. Leading zeros in sh

10. adding leading zeros to a filename

11. leading zeros from dc (desktop calculator)

12. Need to remove leading zeros from file extension

13. ksh- print leading zeroes for job files