📜  在 Bash 中打印不同图案的程序

📅  最后修改于: 2022-05-13 01:57:33.959000             🧑  作者: Mango

在 Bash 中打印不同图案的程序

给定代表行数和列数的数字 N,在 Bash 中打印以下不同的模式。

  • 模式 1:
Input: 6
Output:
     #
    ##
   ###
  ####
 #####
######
  • 使用嵌套循环打印给定的模式。第一个循环代表行,第二个循环代表列。
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


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


输出:

#
   ##
  ###
 ####
#####
  • 模式 2:
Input: 3
Output:
    #
   ###
  #####
  • 使用嵌套循环打印图案的左半部分和右半部分。详细信息在代码中说明:

巴什

# 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

输出:

#
       ###
      #####
     #######
    #########
   ###########
  #############