Various Bash Shell Script to display Pyramid and Pattern
In this, post I am sharing how to create half and full pyramids, Floyd's triangle and Pascal's triangle in bash shell script.
Bash Shell Script to print half pyramid using *
*
* *
* * *
* * * *
* * * * *
Script:
v_rows=5
for((i=1; i<=$v_rows; i++))
do
for((j=1; j<=$i; j++))
do
echo -n "* "
done
echo
done
Bash Shell Script to print half pyramid using numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Script:
v_num=1
v_rows=5
for((i=1; i<=$v_rows; i++))
do
for((j=1; j<=$i; j++))
do
echo -n "$v_num "
v_num=$((v_num + 1))
done
v_num=1
echo
done
Bash Shell Script to print inverted half pyramid using *
* * * * *
* * * *
* * *
* *
*
Script:
v_rows=5
for((i=$v_rows; i>=1; i--))
do
for((j=1; j<=$i; j++))
do
echo -n "* "
done
echo
done
Bash Shell Script to print inverted half pyramid using numbers
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Script:
v_num=1
v_rows=5
for((i=$v_rows; i>=1; i--))
do
for((j=1; j<=$i; j++))
do
echo -n "$v_num "
v_num=$((v_num + 1))
done
v_num=1
echo
done
Bash Shell Script to print full pyramid using *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Script:
v_rows=5
for((i=1; i<=$v_rows; i++))
do
for((j=1; j<=$v_rows - $i; j++))
do
echo -n " "
done
for((j=1; j<=2*$i - 1; j++))
do
echo -n "* "
done
echo
done
Bash Shell Script to print full pyramid using Numbers
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Script:
v_rows=5
v_num=1
for((i=1; i<=$v_rows; i++))
do
for((j=1; j<=$v_rows - $i; j++))
do
echo -n " "
done
v_num=$i
k=1
for((j=1; j<=2*$i - 1; j++))
do
if [ $j -lt $i ];
then
echo -n "$v_num "
v_num=$((v_num + 1))
elif [ $j -eq $i ];
then
echo -n "$v_num "
v_num=$((v_num - 1))
else
echo -n "$v_num "
v_num=$((v_num - 1))
fi
done
echo
done
Bash Shell Script to print Floyd's Triangle
1
2 3
4 5 6
7 8 9 10
Script:
v_num=1
v_rows=4
for((i=1; i<=$v_rows; i++))
do
for((j=1; j<=$i; j++))
do
echo -n "$v_num "
v_num=$((v_num + 1))
done
echo
done
Diamond
Script
#!/bin/bash
echo "Enter the number of rows: "
read n
for((i=1; i<=n; i++))
do
for((j=1; j<=n-i; j++))
do
echo -n " "
done
for((j=1; j<=2*i-1; j++))
do
echo -n "*"
done
echo ""
done
for((i=n-1; i>=1; i--))
do
for((j=1; j<=n-i; j++))
do
echo -n " "
done
for((j=1; j<=2*i-1; j++))
do
echo -n "*"
done
echo ""
done
*
***
*****
*******
*********
*******
*****
***
*
Triangular Shape
#!/bin/bash
echo "Enter the number of rows: "
read n
for((i=1; i<=n; i++))
do
for((j=1; j<=i; j++))
do
echo -n "*"
done
echo ""
done
Post a Comment
Post a Comment