0

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

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by eggi
0

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
0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.