[root@master sh]# cat test1.sh
求100以内的素数
#!/bin/bash
#求100以内的素数
for((i=2;i<=100;i++))
do
yn="y"
for((y=2;y<=$[$i/2];y++))
do
if [ $[$i % $y] == 0 ];then
yn="n"
break
fi
done
if [ "$yn" = "y" ];then
echo $i
fi
done
=================================
2. [root@master sh]# cat test2.sh
#!/bin/bash
#计算开机率
success=0
fail=0
for((i=1;i<50;i++));do
IP="172.16.80.$i"
ping -c1 $IP > /dev/null 2>&1
if [ $? -eq 0 ] ;then
let success=$success+1
else
let fail=$fail+1
fi
done
echo "computer start ratio $(($success*100/50))%"
echo "computer close ratio $(($fail*100/50))%"
====================================
3.[root@master sh]# cat test3.sh
#!/bin/bash
#1*1=1
#1*2=2 2*2=4
#1*3=3 2*3=6 3*3=9
#1*4=4 2*4=8 3*4=12 4*4=16
#.............................
#
#实质被乘数就是行数。乘数的变化:乘数永远小于等于行数。
#乘数是number1 被乘数是line
#
for((line=1;line<=9;line++));do
for((number1=1;number1<=line;number1++));do
echo -n "$number1*$line=$[$number1*$line] "
done
echo
done
=====================================
4. [root@master ~]# cat ./tree1.sh
#!/bin/bash
#三角形:
# 三角形的总行数为total_line(5).
# 每行中有“total_line 减当前行数”个空格,假设当前行为current_line
# 每行中有"当前行*2-1" 个星(star)
total_line=5
for((line=1;line<=total_line;line++));do
for((k=1;k<=total_line-line;k++));do
echo -n " "
done
for((s=1;s<=line*2-1;s++));do
echo -n '*'
done
echo
done
total_line=4
for((line=1;line<=total_line;line++));do
for((k=1;k<=3;k++));do
echo -n " "
done
for((s=1;s<=3;s++));do
echo -n '*'
done
echo
done
看懂的人应该都可以看懂,看不懂的可以在后面留言,我会教你系统学习shell。