shell脚本练习二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本练习二相关的知识,希望对你有一定的参考价值。
打印九九乘法表
#for语句格式
#!/bin/bash
for (( a = 1 ; a <= 9 ; a++ ))
do
for ((b=1 ; b<=$a ; b++))
do
let sum=b*a
echo -ne "$a*$b=$sum "
done
echo ""
done
#while语句格式
#!/bin/bash
a=1
while [ $a -le 9 ]
do
b=1
while [ $b -le $a ]
do
let sum=$a*$b
echo -ne "$a*$b=$sum "
let b++
done
echo ""
let a++
done
打印等边三角
#for语句格式,正三角
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((a=9; a>=$i; a--))
do
echo -n " "
done
for ((b=1; b<=i; b++))
do
echo -n "$i "
done
echo ""
done
#while语句格式,倒三角
#!/bin/bash
i=8
while [ $i -ge 1 ]
do
a=9
while [ $a -ge $i ]
do
echo -n " "
let a--
done
b=1
while [ $b -le $i ]
do
echo -n "$i "
let b++
done
echo""
let i--
done
打印菱形
#while语句格式
#!/bin/bash
i=1
while [ $i -le 9 ]
do
a=9
while [ $a -ge $i ]
do
echo -n " "
let a--
done
b=1
while [ $b -le $i ]
do
echo -n "$i"
let b++
done
c=2
while [ $c -le $i ]
do
echo -n "$i"
let c++
done
echo ""
let i++
done
i=8
while [ $i -ge 1 ]
do
a=9
while [ $a -ge $i ]
do
echo -n " "
let a--
done
b=1
while [ $b -le $i ]
do
echo -n "$i"
let b++
done
c=2
while [ $c -le $i ]
do
echo -n "$i"
let c++
done
echo ""
let i--
done
#for语句格式
for ((i=9; i>0 ; i--))
do
for ((a=1; a<=$i; a++))
do
echo -n " "
done
for ((b=9; b>=$i; b--))
do
echo -n "$i"
done
for ((c=8; c>=$i; c--))
do
echo -n "$i"
done
echo ""
done
for ((i=2; i<=9; i++))
do
for ((a=1; a<=$i; a++))
do
echo -n " "
done
for ((b=9; b>=$i; b--))
do
echo -n "$i"
done
for ((c=8; c>=$i; c--))
do
echo -n "$i"
done
echo ""
done
以上是关于shell脚本练习二的主要内容,如果未能解决你的问题,请参考以下文章