shell脚本--------双重循环

 while循环

直角三角形

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        j=1 
        while [ $j -le $i ]
        do
                echo -n "*"
                let j++ 
        done
        echo
        let i++ 
done 

等腰三角形

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        a=$[ $h - $i ]
        j=1 
        while [ $j -le $a ]
        do
                echo -n " "
                let j++ 
        done

        b=$((2*$i - 1 ))
        k=1 
        while [ $k -le $b ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

倒等腰三角形

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        j=1 
        while [ $j -le $i ]
        do
                echo -n " "
                let j++ 
        done

        k=0 
        a=$(( 2*($h-$i) - 1 ))
        while [ $k -lt $a ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

菱形

#!/bin/bash
# by stanZ
read -p "请输入菱形半径:" h
i=1
while [ $i -le $h ]
do
        a=$[ $h -$i ]
        j=1 
        while [ $j -le $a ]
        do
                echo -n " "
                let j++ 
        done

        b=$((2*$i - 1))
        k=1 
        while [ $k -le $b ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

u=1
while [ $u -lt $h ]
do
        for((n=1;n<=$u;n++))
        do
                echo -n " "
        done

        l=$(( 2*($h-$u)-1 ))
        for((m=1;m<=$l;m++))
        do
                echo -n "*"
        done
        echo
        let u++
done


for循环

倒直角三角形

#!/bin/bash
for ((a=1; a<=9; a++))
do
    for ((b=9; b>=a; b--))
    do
         echo -n '*'
    done
    echo ''
done

直角三角形

#!/bin/bash
for ((a=1; a<=9; a++))
do 
    for ((b=9; b>=a; b--))
    do
         echo -n ' '
    done
    for ((c=1; c<=a; c++))
    do
         echo -n '*'
    done
    echo ''
done

等腰三角形(图形拼接法)

#!/bin/bash
for ((a=1; a<=9; a++))
do 
    for ((b=9; b>=a; b--))
    do
         echo -n ' '
    done
    for ((c=1; c<=a; c++))
    do
         echo -n '*'
    done
    for ((d=2; d<=a; d++))
    do
         echo -n '*'
    done
    echo ''
done

等腰三角形(公式法)

#!/bin/bash
#等腰三角形
#数学公式

for ((a=1; a<=9; a++))
do
    for ((b=9; b>=a; b--))
    do
         echo -n ' '
    done

    i=$[(a * 2) - 1]
    for ((c=1; c<=i; c++))
    do
        echo -n '*'
    done
    echo ''
done

倒等腰三角形

#!/bin/bash
for ((a=9; a>=1; a--))
do
    for ((b=9; b>=a; b--))
    do
         echo -n ' '
    done

    i=$[(a * 2) - 1]
    for ((c=1; c<=i; c++))
    do
        echo -n '*'
    done
    echo ''
done

平行四边形

#!/bin/bash
for ((a=1; a<=9; a++))
do
  for ((c=9; c>=a; c--))
  do
    echo -n ' '
  done
  for ((b=1; b<=9; b++))
  do
    echo -n '*'
  done
  echo ''
done

你可能感兴趣的:(linux,bash,运维,开发语言)