Программы для печати различных шаблонов в Bash
Опубликовано: 16 Февраля, 2022
Given the number N which represents the number of rows and columns, print the different following patterns in Bash.
- Pattern-1:
Input: 6 Output: # ## ### #### ##### ######
- Use nested loop to print the given pattern. The first loop represents the row and the second loop represents the column.
BASH
# Program to print the # given pattern # Static input for N N=5 # variable used for # while loop i=0 j=0 while [ $i - le ` expr $N - 1` ] do j=0 while [ $j - le ` expr $N - 1` ] do if [ ` expr $N - 1` - le ` expr $i + $j` ] then # Print the pattern echo - ne "#" else # Print the spaces required echo - ne " " fi j=` expr $j + 1` done # For next line echo i=` expr $i + 1` done |
Output:
# ## ### #### #####
- Pattern-2:
Input: 3 Output: # ### #####
- Use nested loops to print the left part and right part of the pattern. The details are explained in the code:
BASH
# Program in Bash to # print pyramid # Static input to the # number p=7; for ((m=1; m<=p; m++)) do # This loop print spaces # required for ((a=m; a<=p; a++)) do echo - ne " " ; done # This loop print the left # side of the pyramid for ((n=1; n<=m; n++)) do echo - ne "#" ; done # This loop print right # side of the pryamid. for ((i=1; i<m; i++)) do echo - ne "#" ; done # New line echo ; done |
Выход:
# ### ##### ####### ######### ########### #############