Shell编程——打印空心/实心菱形

一、直接打印出菱行

脚本:

#!/bin/bash
#!/bin/bash
for (( i=1;i<10;i++ ))
do
  if [ $i -le 5 ]
    then
    for (( s=$(( 10-i ));s>i;s-- ))
    do
      echo -n " "
    done
    for (( m=1;m<=$((2*i-1));m++))
    do
      echo -n " *"
    done
    echo ""
  elif [[ $i -gt 5 ]]
    then
    n=$((10-i))
    for (( j=$((10-n));j>n;j--))
    do
      echo -n " "
    done
    for (( m=1;m<=$((2*n-1));m++))
    do
      echo -n " *"
    done
    echo ""
fi
done

执行脚本: 

[root@localhost ~]# ./abc.sh 
         *
       * * *
     * * * * *
   * * * * * * *
 * * * * * * * * *
   * * * * * * *
     * * * * *
       * * *
         *

二、输入菱形半径打印实心菱形

脚本:

#!/bin/bash

read -p "输入菱形半径:" r

for((y=0; y<=2*r; y++))
do
        for((x=0; x<=2*r; x++))
        do
                if ((y>=-x+r && y<=-x+3*r && y<=x+r && y>=x-r))   //实心
                then
                        echo -n "*"
                else
                        echo -n " "
                fi
        done
        echo ""
done

执行脚本:

[root@localhost ~]# ./abc.sh 
输入菱形半径:5
     *     
    ***    
   *****   
  *******  
 ********* 
***********
 ********* 
  *******  
   *****   
    ***    
     *     

三、输入菱形半径打印空心菱形

脚本:

#!/bin/bash

read -p "输入菱形半径:" r

for((y=0; y<=2*r; y++))
do
        for((x=0; x<=2*r; x++))
        do
                if ((y==-x+r || y==-x+3*r || y==x+r || y==x-r))     //空心
                then
                        echo -n "*"
                else
                        echo -n " "
                fi
        done
        echo ""
done

执行脚本:

[root@localhost ~]# ./abc.sh 
输入菱形半径:5
     *     
    * *    
   *   *   
  *     *  
 *       * 
*         *
 *       * 
  *     *  
   *   *   
    * *    
     *     

 

你可能感兴趣的:(Shell编程)