幾個shell初學者必會腳本

[root@master sh]# cat test1.sh shell

求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
donebash


=================================
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))%"
====================================ide


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。學習

相關文章
相關標籤/搜索