hi im new to this shell scripting. I was assigned to develop some code that increased from 0 to 9 in sort of a pyramid. This is what the output should look like.

0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789

here is my code please help because im confused. some advice would be really appreciated. thanks

#!/bin/bash

cnt=0

while [ $cnt -le 9 ]
do
echo $cnt
while [ $cnt -le 9 ]
do
cnt=$((cnt+1))
echo $cnt
done

done

echo

exit

Recommended Answers

All 3 Replies

Let's work with what you have.

counter initialized to zero
while counter less or equal to nine; do
    row initialized to zero
    while row is less or equal to counter; do
        echo -n row #-n suppress echo from adding a newline
        increment row by one
    done
    echo
    increment counter by one
done
finished

thank you so much. now i understand what i was missing. :)

Good deal - I was thinking you could recreate that pyramid doing a little less work, if you have "seq" to work with:

#!/bin/bash

cnt=0

while [ $cnt -le 9 ]
        do
        echo `seq 0 $cnt`
        cnt=$((cnt+1))
done

echo

exit

host # ./program
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

Of course, whatever works best for you is what you should go with :)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.