Login Register






Star pattern in Shell filter_list
Author
Message
Star pattern in Shell #1
I'm learning Shell programming now, and got a homework that wants me to display this :
Quote:*
**
***
****
*****

Now, I tried it using the while loop, as I am still due to learn the for loop syntax in shell.

Here's my code:
PHP Code:
echo "enter size of star" read n i=1 j=1 while [ $i -le $n ] do while [ $j -le $i ] do echo "*" j=`expr $j + 1` done echo -n " " i=`expr $i + 1` done

The result is this:
Quote:*
*
*
*
*

What I have in mind: echo "*" prints * in a new line normally
and echo -n "*" prints * in the same line without going for a new line.
The new line thing is still not clear to me, so Correct me if i'm wrong.

In C or C++ "\n" did the trick for me.
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


Star pattern in Shell #2
I'm learning Shell programming now, and got a homework that wants me to display this :
Quote:*
**
***
****
*****

Now, I tried it using the while loop, as I am still due to learn the for loop syntax in shell.

Here's my code:
PHP Code:
echo "enter size of star" read n i=1 j=1 while [ $i -le $n ] do while [ $j -le $i ] do echo "*" j=`expr $j + 1` done echo -n " " i=`expr $i + 1` done

The result is this:
Quote:*
*
*
*
*

What I have in mind: echo "*" prints * in a new line normally
and echo -n "*" prints * in the same line without going for a new line.
The new line thing is still not clear to me, so Correct me if i'm wrong.

In C or C++ "\n" did the trick for me.
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


RE: Star pattern in Shell #3
Code:
echo "enter size of star" read n i=1 j=1 while [ $i -le $n ] do while [ $j -le $i ] do echo -n "*" j=`expr $j + 1` done echo " " i=`expr $i + 1` j=1 done

try this
[Image: OilyCostlyEwe.gif]


RE: Star pattern in Shell #4
Code:
echo "enter size of star" read n i=1 j=1 while [ $i -le $n ] do while [ $j -le $i ] do echo -n "*" j=`expr $j + 1` done echo " " i=`expr $i + 1` j=1 done

try this
[Image: OilyCostlyEwe.gif]


RE: Star pattern in Shell #5
(09-08-2014, 09:49 AM)Psycho_Coder Wrote:
Code:
echo "enter size of star" read n i=1 j=1 while [ $i -le $n ] do while [ $j -le $i ] do echo -n "*" j=`expr $j + 1` done echo " " i=`expr $i + 1` j=1 done

try this

Thanks a lot. It works.

I got the mistake. my while loop structure was incorrect/misplaced. Then i used -n in the wrong place. Thanks.
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


RE: Star pattern in Shell #6
ok.

Solved and Closed
[Image: OilyCostlyEwe.gif]


RE: Star pattern in Shell #7
ok.

Solved and Closed
[Image: OilyCostlyEwe.gif]