Star pattern in Shell 09-08-2014, 09:20 AM
#1
I'm learning Shell programming now, and got a homework that wants me to display this :
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:
The result is this:
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.
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]](http://i.imgur.com/MUJ8qSW.png)
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)