The below unix shell programming will show you an example on printing pyramid with stars in unix shell programming. This tutorial will also show how to use if else condition, how to use for loop in shell programming. We will also see how to take input from keyboard.

We will be printing pyramid with stars in unix shell programming in two ways – one is stars exist inside and another one is stars do not exist inside the pyramid.

So let’s see the first example where stars exist also inside the pyramid.

The below shell program first takes the depth or level or height from the user for the pyramid. The program itself calculates the number of stars at each level according to the height of the pyramid. We also put the space between two stars to look good.

read -r -p "Enter depth of pyramid: " n
echo "You enetered level: $n"

space=n

for((i=1;i<=n;i++))
do
	space=$((space-1)) #calculate how many spaces should be printed before *              

	for((j=1;j<=space;j++))
	do
		echo -n " " #print spaces on the same line before printing *
	done
	for((k=1;k<=i;k++))
	do
		echo -n "*" #print * on the same line
		echo -n " " #print space after * on the same line
	done
	echo -e #print new line after each row
done

Output

After running the above shell program we get the below output. So here we see that the pyramid is filled with stars whole the area.

        *

       * *

      * * *

     * * * *

    * * * * *

   * * * * * *

  * * * * * * *

 * * * * * * * *

* * * * * * * * *

Now let’s see the example where outer surface of the pyramid is filled with stars. Here also we get the input for level or height of the pyramid and accordingly we put stars on outer surface at each level at two furthest ends.

read -r -p "Enter depth of pyramid: " n
echo "You enetered level: $n"

s=0
space=n

for((i=1;i<=n;i++))
do
	space=$((space-1)) #calculate how many spaces should be printed before *               

	for((j=1;j<=space;j++))
	do
		echo -n " " #print spaces on the same line before printing *
	done
	for((k=1;k<=i;k++))
	do
		if((i==1 || i==2 || i==n))
		then
			echo -n "*" #print * on the same line
			echo -n " " #print space after * on the same line
		elif((k==1))
		then
			echo -n "*" #print * on the same line
		elif((k==i))
		then
			for((l=1;l<=k+s;l++))
			do
							echo -n " " #print spaces on the same line before printing *
			done
			s=$((s+1)) #as pyramid expands at bottom, so we need to recalculate inner spaces
			echo -n "*" #print * on the same line
		fi
	done
	echo -e #print new line after each row
done

Output

Here is the below output we get after running the above shell script.

        *

       * *

      *   *

     *     *

    *       *

   *         *

  *           *

 *             *

* * * * * * * * *

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *