Sinisterly
Star pattern in Shell - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: Star pattern in Shell (/Thread-Star-pattern-in-Shell)



Star pattern in Shell - alok9shm - 09-08-2014

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.


Star pattern in Shell - alok9shm - 09-08-2014

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.


RE: Star pattern in Shell - Psycho_Coder - 09-08-2014

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


RE: Star pattern in Shell - Psycho_Coder - 09-08-2014

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


RE: Star pattern in Shell - alok9shm - 09-09-2014

(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.


RE: Star pattern in Shell - Psycho_Coder - 09-09-2014

ok.

Solved and Closed


RE: Star pattern in Shell - Psycho_Coder - 09-09-2014

ok.

Solved and Closed