print number pyramid with for loop in unix

How can I print number pyramid with for loop(not while only for) in unix like:

1
22
333
4444
55555

---------- Post updated at 09:09 AM ---------- Previous update was at 09:07 AM ----------

I forgot it is in ksh...I wrote a script in bash but it is nt wrkng in ksh...

bash script was:

#!/bin/sh
for ((i=1;i<=5;i++))
do
for ((j=1;j<=i;j++))
do
   echo -e  "$i \c"
done
echo ""
done

SO I need script which will work in ksh please

i=1 j=1
while ((i <= 5)); do
  while ((j <= i)); do
    print -n $i\ 
	j=$((j + 1))
  done
  print
  i=$((i + 1)) j=1 
done

There is a space after the \ character on the fourth line.

Thanks for the reply...

I am looking for the same programe with the for loop....I cannot use while loop......and I have to run the programe in ksh.....

Thanks in advance..

Why not a while loop?

If you get errors with the original code, you should be using a version of ksh different from ksh93. As far as I know ksh88 and pdksh do not support the following syntax:

for ((expr;expr;expr)) ...

As far as I know, all ksh implementations support the while loop ...

#!/bin/ksh
 
for i in {1..5} ; do
j=$i
  while [[ $(( j -= 1 )) -gt -1 ]] ; do
       a=$a$i
    done
echo "$a"
a=""
done
# ./pyramid
1
22
333
4444
55555

The only ksh implementation that supports the above brace expansion is ksh93 ...

It also supports the for loop syntax mentioned in the original post.

So I suppose that this code will not work for the original poster :slight_smile:

:slight_smile: you are right

ksh93 gives error for loop :frowning:

# ./pyramid
./pyramid: line 5: {1..5}: arithmetic syntax error

I change for loop a little :b:

new state is

for i in $(seq 1 5)
 do
....
.....

ksh --version
version sh (AT&T Labs Research) 1993-12-28 q

Now there is a no problem..

# ./pyramid
1
22
333
4444
55555

Hm, no,
I said that only ksh93 (from the ksh family) supports that expansion:

% ksh93 -c 'print ${.sh.version}; print {1..3}'
Version M 93t 2008-11-04
1 2 3

Anyway you're right, not all versions of ksh93 support that syntax:

$ /usr/dt/bin/dtksh -c 'print ${.sh.version}; print {1..3}'
Version M-12/28/93d
{1..3}

Bear in mind that the seq program is not standard too :slight_smile:

:eek::smiley:

Must it be parameterized to support "pyramids" of different heights? If not, I didn't see anything in your posts that precludes using the obvious. :slight_smile:

for i in 1 22 333 4444 55555; do
    echo $i
done

Regards,
Alister

:b:

I vote for this one!

or similarly:

$ for i in 1 2 3 4 5; do printf "%.${i}d\n" 0 | tr 0 $i; done
1
22
333
4444
55555

:slight_smile: