1、等腰三角形
read -p "Please input high:" c
for i in seq 1 $c;do
let space=$c-$i
let num=2$i-1
for y in seq $space;do
echo -e " \c"
done
for z in seq $num;do
echo -e "
\c"
done
echo
done
脚本示例_第1张图片

2、递介求和
read -p "Please input number:" n

if [[ echo $n%2|bc == 1 ]] ; then
let a=n+1
let b=n-1
let c=b/2
let d=a/2
echo $a
echo $b
echo $c
echo $d
echo $a*$c+$d |bc
else
let e=n+1
let f=n/2
echo $e
echo $f
echo $e*$f|bc
fi
脚本示例
3、监控网络连接并发连接数
sleeptime=30
while true ;do
if killall -0 httpd &> /dev/null ;then
true
else
systemctl restart httpd
time=date +"%F %T"
echo " At $time httpd is restarted " >> /app/httpd
fi
sleep $sleeptime
done

4、检测访问失败次数过多IP,并禁用
file="connetion.txt"
dalay=30

ss -nt |grep ESTAB | tr -s " " ":" | cut -d':' -f6 | uniq -c > "${file}"
while read num ip ; do
if [[ $num -gt 3 ]] ; then
iptables -A INPUT -s "$ip" -j REJECT
fi
sleep $dalay
done < "$file"

5、最大值、最小值
declare -i max=0
declare -i min=0

for i in seq 10 ;do
num=$RANDOM
echo $num
if [ $i -eq 1 ] ; then
max=$num
min=$num
fi

    if [ "$num" -lt "$min" ] ;then
            min=$num
    fi

done
echo "======================================"
echo "max" $max
echo "min" $min

脚本示例_第2张图片

6、猜数字
i=echo $[$RANDOM%10+1]
while true ; do
read -p "Please input your number(1--10):" num
if [ $num -gt $i ] ;then
echo "大了。"
elif [ $num -lt $i ]
then
echo "小了。"
elif [ $num -eq $i ]
then
echo "对了。"
exit
else
echo "输入错误。"
exit 2
fi
done
脚本示例_第3张图片

7、成绩
read -p "Please input score:" score
if [[ ! "$score" =~ ^[0-9]+$ ]] ; then
echo "No!"
exit 3
elif [ "$score" -le 59 ];then
echo "bad"
elif [ "$score" -le 80 ];then
echo "good"
elif [ "$score" -le 100 ];then
echo "excellent"
elif [ "$score" -ge 100 ];then
echo "input error"
fi
脚本示例_第4张图片

8、100以内被3整除

#!/bin/bash

for i in seq 1 100 ;do
let j=i%3
if [ $j -eq 0 ] ;then
echo $i
fi
done
脚本示例_第5张图片